What is BSON and MongoDB Extended JSON?
BSON (Binary JSON) is the binary-encoded serialization format used internally by MongoDB to store documents and perform remote procedure calls. While traditional text-based JSON only supports basic types (strings, numbers, booleans, objects, arrays, and null), BSON extends the type system to support high-precision engineering data types such as ObjectId (12-byte unique identifier), ISODate (UTC timestamps), 64-bit Long Integers (NumberLong), 128-bit High-Precision Decimals (Decimal128), and Binary Data (BinData).
When exporting and importing MongoDB documents as plain text, standard JSON cannot distinguish between a plain 24-character string and an ObjectId, or between a text timestamp and a true datetime object. MongoDB Extended JSON solves this by using type-wrapper tags (such as {"$oid": "..."} and {"$date": "..."}). Our converter transforms raw JSON data into ready-to-execute MongoDB Shell (mongosh) scripts, Canonical BSON Extended JSON, PyMongo dictionary definitions, and Mongoose database seeders.
Why MongoDB Developers Need BSON Conversion
Database engineers, backend developers, and data architects use BSON conversion for multiple critical workflows:
- Seeding MongoDB Databases with Type Integrity: Inserting plain JSON objects often inserts dates as strings and large numbers as 32-bit floats. Converting to BSON ensures date indexes (
ISODate) and 64-bit integer counters (NumberLong) are stored with exact binary types. - Executing Interactive Queries in `mongosh` & Compass: Generating copy-pasteable
db.collection.insertMany([...])scripts for rapid testing inside MongoDB Shell, MongoDB Compass, or cloud Atlas terminals. - Migrating API Responses into MongoDB Collections: Transforming JSON payloads received from external webhooks into type-safe documents ready for PyMongo or Mongoose insertion.
- Preventing Date & Number Precision Loss in Aggregations: MongoDB aggregation pipelines (such as
$dateToString,$year, or$matchon timestamp ranges) fail when dates are stored as raw text strings.
Step-by-Step Conversion Example
The following real-world example illustrates how a JSON user document with ISO timestamps and 64-bit financial balances is translated into MongoDB Shell commands and Canonical BSON representations.
Input: Standard JSON Document
{
"_id": "507f1f77bcf86cd799439011",
"username": "dev_alex",
"email": "alex@mongodb.org",
"accountBalance": 4500000000,
"createdAt": "2026-08-15T14:30:00.000Z"
}
Output: MongoDB Shell (mongosh) Insert Command
// MongoDB Shell (mongosh) Insert Command
db.users.insertOne({
_id: ObjectId("507f1f77bcf86cd799439011"),
username: "dev_alex",
email: "alex@mongodb.org",
accountBalance: NumberLong("4500000000"),
createdAt: ISODate("2026-08-15T14:30:00.000Z")
});
Output: Canonical BSON Extended JSON
{
"_id": { "$oid": "507f1f77bcf86cd799439011" },
"username": "dev_alex",
"email": "alex@mongodb.org",
"accountBalance": { "$numberLong": "4500000000" },
"createdAt": { "$date": { "$numberLong": "1786804200000" } }
}
BSON Data Type Specification Reference
Our converter automatically detects and maps standard JSON primitives to their appropriate BSON specifications:
- ObjectId (
$oid): 24-character hexadecimal strings in the_idproperty are converted to 12-byte BSON ObjectIds. - ISODate (
$date): ISO 8601 formatted datetime strings (e.g.YYYY-MM-DDTHH:mm:ss.sssZ) are converted to native 64-bit integer millisecond UTC timestamps. - 64-bit Integer (
$numberLong): Large integer numbers exceeding 32 bits ($> 2,147,483,647$) are preserved as 64-bit longs to prevent IEEE-754 precision loss. - Binary Data (
$binary): Base64 encoded binary assets are mapped to binary subtype containers.
BSON Decimal128 vs. IEEE-754 Double Precision
In financial and e-commerce applications, standard floating-point numbers in JavaScript and JSON are vulnerable to rounding errors (e.g. 0.1 + 0.2 = 0.30000000000000004).
- Decimal128 (
$numberDecimal): Implements IEEE 754-2008 decimal floating-point arithmetic with 34 decimal digits of precision, preventing currency rounding discrepancies. - BSON Double (
$numberDouble): Standard 64-bit binary floating point used for scientific calculations where exact decimal precision is not mandatory.
MongoDB Indexing & Query Optimization with Typed BSON
Storing data with explicit BSON data types is essential for high-speed indexing:
- TTL (Time-To-Live) Indexes: MongoDB TTL indexes (e.g.
expireAfterSeconds: 3600) exclusively monitor fields stored as nativeISODatetimestamps. If dates are stored as raw text strings, TTL expiration fails completely. - Geospatial 2dsphere Indexes: Storing coordinate GeoJSON objects enables sub-millisecond proximity queries (
$near,$geoWithin). - Collation & Sorting: Numbers stored as strings sort alphabetically (
"10"before"2"), whereas BSON numerical types sort with mathematical accuracy.
100% Client-Side Privacy & Air-Gapped Security Guarantee
Transforming production MongoDB database records, user authentication schemas, and internal collections requires total security. Uploading confidential MongoDB documents to cloud conversion web services creates significant compliance and data leakage risks.
JSON Empire guarantees total browser isolation:
- All BSON type analysis, regular expression parsing, and script generation run 100% locally on your computer's CPU.
- Zero HTTP network requests are made. No database data ever touches external servers.
- Full offline and air-gapped support: works seamlessly in secure database subnets without an active internet connection.
Frequently Asked Questions
What is the difference between Mongo Shell format and Canonical BSON?
Mongo Shell format uses native helper functions (ObjectId("..."), ISODate("...")) designed for direct execution in mongosh terminals. Canonical BSON Extended JSON uses strict JSON type objects ({"$oid": "..."}) designed for automated import tools like mongoimport.
How can I export a Python PyMongo or Node.js Mongoose seed script?
Select "Python PyMongo Script" or "Node.js Mongoose Seed" from the Format dropdown in the toolbar above, then click the Generate button to produce ready-to-run database initialization code.
How can I download the generated script?
Click the "💾 Download Script" button in the workspace panel to save a standalone .js, .py, or .json file directly to your computer.
Can I reverse BSON back into clean JSON?
Yes. Use our companion tool BSON to JSON Converter (Tool 26) to strip MongoDB type wrappers back into clean standard JSON objects.