jsontoolskit.org
JSON and JSON Schema utilities, in the browser
Say hi →

MongoDB Extended JSON

canonical · relaxed · mongosh syntax · plain JSON — any direction

What comes out of MongoDB is not JSON. ObjectId("…") is not valid JSON, and neither is {"$date":{"$numberLong":"1600000000000"}} to anything downstream that expects a timestamp. This reads all three dialects — shell output, Extended JSON v2 canonical and relaxed — and writes whichever one you need, keeping the type information rather than flattening it by accident.

Paste a document — shell output, canonical or relaxed Extended JSON, or plain JSON.

How to use it

  1. Paste a document or an array of them. The input dialect is worked out for you — you do not pick it.
  2. Choose the output. Plain JSON for anything outside the Mongo ecosystem; relaxed for a readable dump; canonical when a driver or a test fixture has to round-trip exactly; mongosh syntax to paste back into a shell.
  3. Read the status line. It counts the BSON types it found — 3 ObjectId, 2 Date, 1 Decimal128 — and warns whenever a value could not survive the conversion exactly.

The three dialects, and why there are three

BSON has types JSON does not: an ObjectId, a 64-bit integer, a Decimal128, a date, a binary blob, a regex with flags. Extended JSON is the agreed way to write them in JSON, and it comes in two modes.

What it reads

All of these arrive as the same typed document, whichever way they were written:

The scan that does this is string-aware: a document whose note field contains the text ObjectId("x") comes out with that text intact. Rewriting blindly with a regular expression is the obvious way to build this tool and it corrupts exactly the documents that mention their own ids.

Where a conversion cannot be exact — and what happens instead

Two BSON types do not fit in a JSON number, and this is the part other converters get quietly wrong.

Going the other way, plain JSON carries no types, so the extended dialects have to infer: an integer within int32 range becomes $numberInt, a larger one $numberLong, anything with a fractional part $numberDouble. Strings are left alone unless you tick the two detect options — because a 24-character hex string is often a hash, and turning it into an ObjectId would be a guess with consequences.

Examples

Shell output in:

{ _id: ObjectId("507f1f77bcf86cd799439011"),
  created: ISODate("2020-09-13T12:26:40Z"),
  price: NumberDecimal("9.99"),
  tags: /^sale/i }

Plain JSON out:

{
  "_id": "507f1f77bcf86cd799439011",
  "created": "2020-09-13T12:26:40.000Z",
  "price": "9.99",
  "tags": "/^sale/i"
}

Canonical Extended JSON out — the same document, ready to load back:

{
  "_id": { "$oid": "507f1f77bcf86cd799439011" },
  "created": { "$date": { "$numberLong": "1600000000000" } },
  "price": { "$numberDecimal": "9.99" },
  "tags": { "$regularExpression": { "pattern": "^sale", "options": "i" } }
}

FAQ

Is my data uploaded?

No. Everything runs in this page — the parser, the type inference and the writer are all local, and no library is fetched for them. Production documents are exactly the thing you should not paste into a server-side converter.

Can it handle a whole mongoexport file?

A JSON array, yes. mongoexport's default output is one document per line, which is JSONL rather than JSON — convert that with JSONL to JSON first, or paste one line at a time.

Why is my date one hour out?

It is not — it is in UTC. BSON dates are milliseconds since the epoch with no timezone, so both the ISO string and the epoch number here are UTC. A local-time rendering is your application's decision, not the document's.

What does the shell output do with a Decimal128?

Writes NumberDecimal("9.99"), which is what you would have typed. Paste it into mongosh and the type survives; paste a bare 9.99 and you have inserted a double.

My document has a key called $set. Does that break it?

No. A wrapper is only recognised when the object has exactly the shape the spec defines — {"$oid": "…"} alone, for example. An update document full of $set and $inc passes through as an ordinary object.