Skip to content
Sealpaste

Delete all recent work?

This removes the 0 items saved in this browser. Your settings stay.

Stop saving recent work?

New work won't be saved in this browser. The 0 items already saved can stay or go.

MongoDB Extended JSON ↔ JSON

Extended JSON

Paste Extended JSON, or

CtrlV paste · Ctrl↵ run now

Plain JSON
Ready
Ln 1, Col 1
Runs in your browser. Nothing you paste is uploaded unless you share it.

Convert MongoDB Extended JSON or mongosh output (ObjectId, ISODate, NumberLong) to plain JSON, or JSON to EJSON

Paste a document from mongosh, Compass or mongoexport and get plain JSON, or turn plain JSON into Extended JSON ready for mongoimport.

How to use it

  1. Choose the direction.
  2. Paste a document or an array. mongosh syntax (ObjectId('…'), ISODate(…), unquoted keys, single quotes) is accepted.
  3. For JSON → EJSON, choose relaxed or canonical and which strings become ObjectIds and dates.

Examples

mongosh output to plain JSON

Input
{ _id: ObjectId('65f1c0a8e4b0a1b2c3d4e5f6'), total: NumberDecimal('149.90'), createdAt: ISODate('2024-03-13T15:05:12Z') }
Result
{
  "_id": "65f1c0a8e4b0a1b2c3d4e5f6",
  "total": "149.90",
  "createdAt": "2024-03-13T15:05:12.000Z"
}

Decimal128 becomes a string so no cents are lost.

Plain JSON to relaxed EJSON

Input
{"_id": "65f1c0a8e4b0a1b2c3d4e5f6", "createdAt": "2024-03-13T15:05:12Z"}
Result
{
  "_id": { "$oid": "65f1c0a8e4b0a1b2c3d4e5f6" },
  "createdAt": { "$date": "2024-03-13T15:05:12Z" }
}

Plain JSON to canonical EJSON

Input
{"_id": "65f1c0a8e4b0a1b2c3d4e5f6", "qty": 3, "price": 12.5, "createdAt": "2024-03-13T15:05:12Z"}
Result
{
  "_id": { "$oid": "65f1c0a8e4b0a1b2c3d4e5f6" },
  "qty": { "$numberInt": "3" },
  "price": { "$numberDouble": "12.5" },
  "createdAt": { "$date": { "$numberLong": "1710342312000" } }
}

Canonical keeps each number’s type, so 3 goes back into MongoDB as an int and 12.5 as a double.

How BSON types become plain JSON

BSON / shellPlain JSON
ObjectId("…")"65f1c0a8…" (hex string)
ISODate("…") / $date"2024-03-13T15:05:12.000Z" (ISO string)
NumberInt, NumberLong, Doublenumber (exact, also beyond 2^53)
NaN, Infinity"NaN", "Infinity" (text: JSON has no such numbers)
NumberDecimal / $numberDecimal"149.90" (string, keeps precision)
UUID("…"), subtype 4"0e5e4d7f-…" (UUID string)
BinData / $binaryBase64 string
Timestamp(t, i){"t": …, "i": …}
/regex/i"/regex/i" (options in alphabetical order: xmi becomes imx)

Every $ wrapper

Each BSON type has one wrapper. Relaxed mode drops the wrapper only where plain JSON says the same thing.

TypeCanonicalRelaxed
ObjectId{"$oid": "65f1c0a8…"}Same
Date{"$date": {"$numberLong": "1710342312000"}}{"$date": "2024-03-13T15:05:12Z"}
32-bit integer{"$numberInt": "3"}3
64-bit integer{"$numberLong": "9007199254740993"}9007199254740993
Double{"$numberDouble": "12.5"}12.5 (NaN and Infinity stay wrapped)
Decimal128{"$numberDecimal": "149.90"}Same
Binary, UUID{"$binary": {"base64": "…", "subType": "04"}}Same
Regular expression{"$regularExpression": {"pattern": "^a", "options": "i"}}Same
Timestamp{"$timestamp": {"t": 1710342312, "i": 1}}Same
MinKey, MaxKey{"$minKey": 1}, {"$maxKey": 1}Same

Sources: MongoDB Extended JSON v2 spec

Relaxed vs canonical Extended JSON

Relaxed mode writes numbers as plain JSON numbers and dates as ISO strings inside {"$date": …}; it is readable and what mongoexport writes by default. A date before 1970 or after 9999 is written as milliseconds, {"$date": {"$numberLong": "…"}}, in both modes, as the spec says. Years past 9999 are read from ISO strings with six digits and a sign, such as +010000-01-01T00:00:00Z.

Canonical mode wraps every number with its exact type ({"$numberInt": "5"}, {"$numberDouble": "1.5"}) so a round trip through JSON keeps int vs double vs long exactly. Use it when types matter, e.g. for fixtures and migrations.

Sources: MongoDB Extended JSON v2 spec

Questions

Why can’t JSON.parse read my mongosh output?
mongosh prints JavaScript, not JSON: unquoted keys, single quotes and helpers like ObjectId(…) and ISODate(…). This converter understands that syntax and turns it into real JSON.
Which strings become ObjectIds in JSON → EJSON?
24-character hex strings in _id, id and fields ending in Id, ID or _id (customerId, user_id). Other hex-looking strings stay strings. Turn the option off to keep every string as is.
Is my data uploaded?
No. Everything runs in your browser, so your data stays on your device. Nothing you paste is uploaded unless you share a result, which is locked with your password first. Conversion uses the official bson library, with no database connection.

Related tools