Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force mongodb to output strict JSON

I want to consume the raw output of some MongoDB commands in other programs that speak JSON. When I run commands in the mongo shell, they represent Extended JSON, fields in "shell mode", with special fields like NumberLong , Date, and Timestamp. I see references in the documentation to "strict mode", but I see no way to turn it on for the shell, or a way to run commands like db.serverStatus() in things that do output strict JSON, like mongodump. How can I force Mongo to output standards-compliant JSON?

There are several other questions on this topic, but I don't find any of their answers particularly satisfactory.

like image 211
whereswalden Avatar asked Aug 19 '15 13:08

whereswalden


People also ask

How get JSON data from MongoDB?

Format Option: Choose JSON-mongo shell/ JSON-mongoexport. Target: Choose between clipboard/file & make sure the file path is defined. Others: Choose whether to export with commas between documents or export as a document array. Output Preview: Displays the final JSON document.

Is MongoDB good for JSON?

The best database for JSON This makes the MongoDB database the best natural fit for storing JSON data. You can store details of an entire object in one document, making it easier to view and query. MongoDB is the most popular JSON database as it offers many other benefits, like: Flexible schema.

How do I run a JSON script in MongoDB?

Open the Import Wizard. Then, choose JSON as the import format and click OK. Click on + to add JSON source documents, – to remove them, or the clipboard icon to paste JSON data from the clipboard. Here we will add the JSON source document, Rainfall-Data.

What is extended JSON?

Abstract. MongoDB Extended JSON is a string format for representing BSON documents. This specification defines the canonical format for representing each BSON type in the Extended JSON format. Thus, a tool that implements Extended JSON will be able to parse the output of any tool that emits Canonical Extended JSON.


2 Answers

The MongoDB shell speaks Javascript, so the answer is simple: use JSON.stringify(). If your command is db.serverStatus(), then you can simply do this:

JSON.stringify(db.serverStatus()) 

This won't output the proper "strict mode" representation of each of the fields ({ "floatApprox": <number> } instead of { "$numberLong": "<number>" }), but if what you care about is getting standards-compliant JSON out, this'll do the trick.

like image 175
whereswalden Avatar answered Oct 14 '22 07:10

whereswalden


I have not found a way to do this in the mongo shell, but as a workaround, mongoexport can run queries and its output uses strict mode and can be piped into other commands that expect JSON input (such as json_pp or jq). For example, suppose you have the following mongo shell command to run a query, and you want to create a pipeline using that data:

db.myItemsCollection.find({creationDate: {$gte: ISODate("2016-09-29")}}).pretty() 

Convert that mongo shell command into this shell command, piping for the sake of example to `json_pp:

mongoexport --jsonArray -d myDbName -c myItemsCollection -q '{"creationDate": {"$gte": {"$date": "2016-09-29T00:00Z"}}}' | json_pp 

You will need to convert the query into strict mode format, and pass the database name and collection name as arguments, as well as quote properly for your shell, as shown here.

like image 26
jbyler Avatar answered Oct 14 '22 08:10

jbyler