Re: tojson/printjson output parsing

507 views
Skip to first unread message
Message has been deleted
Message has been deleted
Message has been deleted

brandonblack

unread,
Sep 13, 2012, 5:58:41 PM9/13/12
to mongod...@googlegroups.com

MongoDB actually uses what we call Mongo Extended JSON which differs a bit from the vanilla JSON standard. There are however a few different document output modes that MongoDB supports including a JSON "Strict" mode that will resolve the parsing issues you're experiencing.

In strict mode, the value of _id for your document will be changed from ObjectId("<id>") to something like {"$oid": "<id>"} to allow for valid JSON parsing. You can read through the link above to see the differences between each output mode in detail.

What kind of queries are you trying to run against the database from your script? If you're trying to access regular data from a document collection you actually have a couple of options you could use from the command line.

1.) Use MongoDB's simple REST interface (this is enabled with the --rest flag when you run mongod

curl "http://127.0.0.1:28017/exampleDatabase/exampleCollection/?limit=10"

If you need to perform more complex interactions (write actions, more complex queries), you should consider running one of the community-built RESTful interfaces for MongoDB.

2.) Use the mongoexport tool from a shell script.

mongoexport --db exampleDatabase --collection exampleCollection | grep "Katie"

connected to: 127.0.0.1

exported 10 records

{ "_id" : { "$oid" : "505111c6b0f594a74b661da3" }, "name" : "Katie"}


mongoexport --db exampleDatabase --collection exampleCollection -q "{name: /^K/i}" -o export.json

mongoexport --db exampleDatabase --collection exampleCollection -o export.json

You can read more info about the mongoexport tool here.

It's also worth noting that both of these options should be much faster/more performant than running db.eval() 


On Wednesday, September 12, 2012 8:59:34 AM UTC-7, cvsdave wrote:
I am trying to parse printjson output using a JSON parser, JSON.sh ( a BASH script lib JSON parser: https://github.com/rcrowley/json.sh ).  The parser chokes on right-hand values such as ObjecID("123456789").  I took a look at the JSON spec (http://www.ietf.org/rfc/rfc4627.txt) and now question whether this is well-formed JSON, as it does not appear to meet the criteria" 
 
  "...A JSON value MUST be an object, array, number, or string, or one of
   the following three literal names:

      false null true ..."
 
I am running queries from a BASH shell script against Mongo, and trying to use the output by using the JSON parser.
 
--am I incorrect about the JSON standard?
--is there a better way to go with BASH shell scripts?  (there seems to be no driver)
--do I need to modify the parser to recognize the printjson output?  When I look at the BSON spec, it seems aimed at the bytestream output, and I think I am actually looking at a printjson issue rrather than a BSON issue.
 
Any help or comments would be appreciated. I can't use an alternative language (e.g., Python, Ruby, etc)



Reply all
Reply to author
Forward
0 new messages