zpoley/json-command

Syntax errors when parsing

Opened this issue · 5 comments

Hi there.

I've come across errors when processing json from a couchdb. I'll use the standard _users database for example.

$ curl "localhost:5984/_users/_all_docs" 
{"total_rows":1,"offset":0,"rows":[
{"id":"_design/_auth","key":"_design/_auth","value":{"rev":"1-c44fb12a2676d481d235523092e0cec4"}}
]}

Now when sending through json, this is the output:

     $ curl "localhost:5984/_users/_all_docs" | json
 {
    "id": "_design/_auth",
    "key": "_design/_auth",
    "value": {
        "rev": "1-c44fb12a2676d481d235523092e0cec4"
    }
}

Here's the debugging output:

$ curl "localhost:5984/_users/_all_docs" | json -d
ex: {
    "stack": "SyntaxError: Unexpected end of input\n    at Object.parse (native)\n    at [object Object].processObjects (/usr/local/lib/node/.npm/json/0.0.8/package/lib/jsonCommand.js:351:31)\n    at Socket.<anonymous> (/usr/local/lib/node/.npm/json/0.0.8/package/lib/jsonCommand.js:443:40)\n    at Socket.emit (events.js:64:17)\n    at Socket._onReadable (net.js:667:31)\n    at IOWatcher.onReadable [as callback] (net.js:177:10)",
    "arguments": [],
    "type": "unexpected_eos",
    "message": "Unexpected end of input"
}
{
    "id": "_design/_auth",
    "key": "_design/_auth",
    "value": {
        "rev": "1-c44fb12a2676d481d235523092e0cec4"
    }
}
ex: {
    "stack": "SyntaxError: Unexpected token ]\n    at Object.parse (native)\n    at [object Object].processObjects (/usr/local/lib/node/.npm/json/0.0.8/package/lib/jsonCommand.js:351:31)\n    at Socket.<anonymous> (/usr/local/lib/node/.npm/json/0.0.8/package/lib/jsonCommand.js:448:18)\n    at Socket.emit (events.js:61:17)\n    at Socket._onReadable (net.js:652:51)\n    at IOWatcher.onReadable [as callback] (net.js:177:10)",
    "arguments": [
        "]"
    ],
    "type": "unexpected_token",
    "message": "Unexpected token ]"
}

Hi Alex,

This seems to be an issue with the newlines in the couchdb json. You can try the following command which replaces the newlines with space:

curl "localhost:5984/_users/_all_docs" | tr '\n' ' ' | json

Please let me know if this doesn't solve your problem or if you have other questions.

Thanks,
Zach

Yes, this works. The JSON that CouchDB returns is valid, though and suggests that the parser should take care of this, and not the human.

Liking the app though, and for the time being, I'll just alias json to your quickfix

Great! I agree that the parser should handle that. This is one of the corners that I decided to cut the first time around. I will think more about how to handle it.