11

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.

jmcollin92
  • 2,896
  • 6
  • 27
  • 49

3 Answers3

24

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())"

Gökhan Şengün
  • 583
  • 5
  • 18
7

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.

jmcollin92
  • 2,896
  • 6
  • 27
  • 49
5

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"),
profesor79
  • 9,213
  • 3
  • 31
  • 52
  • 2
    Thank's for your answer but I need this for mongo shell command output and not in data export. For instance, I need to parse the result of the "rs.config()" which show the ReplicatSet configuration. The exact I would like to parse is the following: mongo --eval "rs.config()" – jmcollin92 Sep 12 '16 at 11:29
  • See http://stackoverflow.com/questions/32097209/force-mongodb-to-output-strict-json. I ran into the exact same issue! – whereswalden Dec 31 '16 at 21:52