Q: I want to cut and paste plain JSON from Studio 3T into other applications but when I view a document I see numeric fields appear such as NumberInt(4). What can I do about it?
Welcome to the latest Studio 3T Ask Manatees Anything – #Studio3T_AMA. Want to know how to use Studio 3T to make your MongoDB life better? Just tweet and use #Studio3T_AMA or email [email protected].
A: What you are seeing in the JSON view is, by default, the Mongo shell’s version of the data in your database, as JSON. It is also called Extended JSON and there’s a couple of versions of it. Extended JSON is all about preserving type information in something that’s quite like, but not quite JSON.
If you want to see your data in plain, un-extended JSON, then the JSON:Pure view option, new in Studio 3T 2021.4, will be very useful.
When you are viewing a collection in the JSON view, clicking the cog next to the view type will display the view menu. Your view will likely be set to JSON: Mongo Shell Format. In that menu, you’ll also find JSON: Pure, without MongoDB types.
Now the view will be a plain, typeless JSON, ideal for cutting and pasting.
If you select View Document, either from the right click menu, icon or pressing F3, you’ll also find a similar menu in that view.
Set it to JSON: Pure and copy and paste JSON data anywhere.
Why Not Pure JSON?
There is an important warning that goes with JSON: Pure; you are losing information when you switch to it, specifically what type the data is. JSON only supports strings and numbers while MongoDB’s BSON format allows data to be stored as, for example, integers, double, 128-bit decimals, strings, binary, dates and objectids.
That means when you display a field where the field type is integer, while JSON: Pure would show it as
{ "Field": 5 }
If you updated a document with that format of data, MongoDB would interpret the field as a double (the default) and discard the integer type data.
JSON: Mongo Shell Format would show it as:
{ "Field": NumberInt(5) }
When you update using that format, the integer nature of the field is retained.
And that’s why when you call up the Edit Document operation in the Collections View, you’ll find there’s no option to change the format. It’s locked down to JSON: Mongo shell to ensure that no type data is lost. That is especially important for id fields which, by default, are ObjectId
types are wrapped withObjectId
(). Similarly, dates, which are Date
types are wrapped with ISODate
(). This is done so they can be efficiently parsed and processed.
Those other JSON views
Finally, for the curious, those JSON views explained:
- JSON: Mongo Shell Format – the default format; anything that is not a natural JSON type (string or number) is wrapped in an object constructor such as
ObjectID
,NumberInt
,Decimal128
orISODate
. These wrappers tend not to be parsable by generic JSON tools. Best for most MongoDB related things.
- JSON: Simple Format – this format attempts to make all fields a string or number, except for fields which are
ObjectID
orDate
types. Those, and only those, are wrapped in a constructor. They still tend not to be parsable by generic JSON tools. Useful in some cases, but JSON: Pure might be the better option.
- JSON: MongoExport Format – This format retains the type information but entirely in JSON. That’s done by turning typed data into a JSON object. So
ObjectId("613f1b49d6b66c04ee84f370")
becomes{ "$oid" : "613f1b49d6b66c04ee84f370" }
. This is also called strict Extended JSON. Any application reading it will need to know how to interpret the type objects. Generic JSON tools will be able to read the files. Best for interchange between MongoDB databases.
And as we’ve discussed…
- JSON: Pure – This format discards the type data and renders the file in a pure JSON format. So,
ObjectId("613f1b49d6b66c04ee84f370")
becomes the string"613f1b49d6b66c04ee84f370"
. Generic JSON tools can happily parse and work with this data but watch out for loss of precision when handling numbers and no indications of what fields are dates. Best for interoperability with JSON tools and JSON-consuming systems.
Plain JSON is Pure JSON
So the lesson in this AMA is Plain JSON is also Pure JSON in Studio 3T. Pure JSON views take out all the type information from MongoDB’s Extended JSON. It does leave you with JSON ready to cut and paste. As long as you are aware that the type information is gone and the precision of some values may be compromised, you should be good to go.