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
Choose the direction. Paste a document or an array. mongosh syntax (ObjectId('…'), ISODate(…), unquoted keys, single quotes) is accepted. For JSON → EJSON, choose relaxed or canonical and which strings become ObjectIds and dates.
Examples
mongosh output to plain JSON
{ _id: ObjectId('65f1c0a8e4b0a1b2c3d4e5f6'), total: NumberDecimal('149.90'), createdAt: ISODate('2024-03-13T15:05:12Z') }{
"_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
{"_id": "65f1c0a8e4b0a1b2c3d4e5f6", "createdAt": "2024-03-13T15:05:12Z"}{
"_id": { "$oid": "65f1c0a8e4b0a1b2c3d4e5f6" },
"createdAt": { "$date": "2024-03-13T15:05:12Z" }
}Plain JSON to canonical EJSON
{"_id": "65f1c0a8e4b0a1b2c3d4e5f6", "qty": 3, "price": 12.5, "createdAt": "2024-03-13T15:05:12Z"}{
"_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 / shell | Plain JSON |
|---|---|
ObjectId("…") | "65f1c0a8…" (hex string) |
ISODate("…") / $date | "2024-03-13T15:05:12.000Z" (ISO string) |
NumberInt, NumberLong, Double | number (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 / $binary | Base64 string |
Timestamp(t, i) | {"t": …, "i": …} |
/regex/i | "/regex/i" (options in alphabetical order: xmi becomes imx) |
Every $ wrapper
| Type | Canonical | Relaxed |
|---|---|---|
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
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.