Posts Tagged ‘json’

Invalid JSON in CouchDB

03 Dec

I recently upgraded from Ubuntu 12.04 to 12.10 and Ubuntu automatically upgraded my CouchDB installation from 1.0.1 to 1.2.0. After the upgrade I started getting the error message “Bad request… Invalid JSON” anytime I try to add or query an existing design document.

A lot my code depends on CouchDB so this was a big deal, after hours of googling, still no solution until I found the JSONlint test. I realized that the old JSON parser in CouchDB 1.0.1 would parse invalid JSON with no problems but the parser in CouchDB 1.2.0 would throw an error.

This code below is invalid JSON but works fine on CouchDB 1.0.1. It will throw the ‘Invalid JSON’ error on CouchDB 1.2.0

 "language" : "javascript",
 "views" :{
   "age" : {
     "map" : "function(doc){

To work on CouchDB 1.2.0 it has to be valid JSON like the code below

    "language": "javascript",
    "views": {
        "age": {
            "map": "function(doc){ if(doc.age) emit(doc.age,doc); }"

This small change fixes the problem. You can always test your JSON for validity here:

Premium Wordpress Plugin