Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jq cannot parse MongoDB output document [duplicate]

Tags:

json

mongodb

jq

Jq is a wonderfull tool to deals with JS document in bash. But I can't use to parse some MongoDB output documents due to function like added by Mongodb. Example of MongoDB Json return:

{
    "_id" : "example",
    "version" : 23,
    "members" : [
        {
            "_id" : 0,
            "host" : "192.168.0.1:27017",
            "slaveDelay" : NumberLong(0),   <---- Here jq failed to parse this line
            "votes" : 1
        }
]}

The error given by jq is:

parse error: Invalid numeric literal at line 15, column 32

Any help would be greatly appreciated.

like image 468
jmcollin92 Avatar asked Sep 12 '16 09:09

jmcollin92


3 Answers

I know I am a little too late here but Mongo shell has built-in JavaScript support. So you can use JSON.stringify.

For the specific example above, you can use below

mongo --quiet --eval "JSON.stringify(rs.config())"

like image 184
Gökhan Şengün Avatar answered Nov 20 '22 07:11

Gökhan Şengün


this is caused because mongo uses BSON in the backend see this

using mongoexport with json option will allow to generate clean json files:

 "CreatedDate":{"$date":"2016-08-13T01:01:20.833Z"}

instead of:

 "CreatedDate" : ISODate("2016-08-13T01:01:20.833Z"),
like image 31
profesor79 Avatar answered Nov 20 '22 06:11

profesor79


A workaround that could be sufficient for certain use is to transform the output of the mongo shell command. I use this pattern for a workaround:

mongo --quiet --eval "rs.config()" | sed -e 's/: [a-zA-Z]*(\(.*\))/: "\1"/' | jq '.'

It removes all functions call after the ':' and keep only the value.

like image 6
jmcollin92 Avatar answered Nov 20 '22 06:11

jmcollin92