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.
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())"
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"),
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With