BSON Encode/Decode Online
Convert between Extended JSON and BSON, work with MongoDB types (ObjectId, Date, Binary, Decimal128), generate fresh ObjectIds, and diff the raw bytes side by side — all in your browser.
What is BSON Encode/Decode Online?
BSON (Binary JSON) is the binary serialization format MongoDB uses to store documents and move data across its wire protocol. It extends JSON with types like ObjectId, Date, and Int64 that plain text cannot represent, and is designed for efficient storage and scanning. This toolbox turns BSON into something you can actually inspect: it converts Extended JSON — including ObjectId, Date, Binary, Decimal128, and Int64 values — into BSON bytes (hex or base64), decodes BSON back into readable relaxed or canonical Extended JSON, generates fresh ObjectIds for test documents, and diffs both byte streams as side-by-side hex dumps with offset rulers and top-level type annotations.
Zero-Server Tool Data Guarantee
All encoding and decoding happens locally in your browser. Your data is never sent to any server.
How to Use
Choose Your Mode
Select 'Encode' to turn Extended JSON into BSON binary, or 'Decode' to convert BSON bytes back to Extended JSON. Drop a file instead of pasting — decode mode reads the raw bytes.
Set Encoding Type
Pick either 'Hex' or 'Base64' as the text representation for your binary BSON data.
Insert an ObjectId (encode)
Click 'ObjectId' to insert a fresh {"$oid":"…"} value at the cursor — handy for building test documents with _id fields.
Choose an Output Style (decode)
Leave 'Canonical' unticked for relaxed Extended JSON (readable {"$oid"} and {"$date"} forms), or tick it for canonical output that spells out every number type.
Diff the Bytes
Open the Hex Dump Diff tab to see Extended JSON and its BSON encoding side by side with offset rulers and field-count annotations.
Common Use Cases
MongoDB Document Inspection
Decode BSON documents exported from MongoDB to inspect ObjectId, Date, and Binary values in readable Extended JSON.
Test Fixture Generation
Insert fresh ObjectIds at the cursor to build realistic test documents with _id fields.
Type Mapping Testing
Verify how JSON numbers and strings map to BSON Int32, Int64, Double, and Decimal128 before writing data.
Database Migration
Convert between JSON and BSON when migrating data between MongoDB instances or to different databases.
Wire Protocol Debugging
Inspect raw BSON bytes from MongoDB wire protocol captures during network analysis.
Implementation Examples
import {BSON, EJSON, ObjectId} from "bson";const toHex = (bytes) => [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");// Round-trip test: doc -> BSON bytes -> docconst doc = {_id: new ObjectId("507f1f77bcf86cd799439011"), name: "sensor-7", active: true};const bytes = BSON.serialize(doc);console.log(toHex(bytes)); // 32000000075f696400507f1f77bcf86cd799439011026e616d65...const decoded = BSON.deserialize(bytes);console.log(decoded._id.toHexString()); // 507f1f77bcf86cd799439011// Extended JSON keeps ObjectId/Date/Binary types portable and readableconst ej = EJSON.stringify(doc, null, 2);console.log(ej); // {"_id":{"$oid":"507f1f77bcf86cd799439011"},...}// Back to BSON objects from Extended JSONconsole.log(EJSON.parse(ej)._id.toHexString()); // 507f1f77bcf86cd799439011
BSON vs JSON vs MessagePack
| Feature | BSON | JSON | MessagePack |
|---|---|---|---|
| Format | Binary | Text | Binary |
| Data types | Extended (ObjectId, Date, Int64, Decimal128) | Basic (string, number, bool, null, array, object) | Basic + raw bytes, ext types |
| Self-describing? | Yes (length-prefixed) | No | Partially |
| Size | Often similar to JSON | Baseline | Compact |
| Primary use | MongoDB storage | APIs, config | Performance-critical APIs |
| Parse speed | Fast | Slow | Very fast |
Production Best Practices & Security
Frequently Asked Questions
BSON (Binary JSON) is a binary serialization format that extends JSON with additional data types. It was created for MongoDB and serves as both its on-disk storage format and its network transfer protocol.
BSON adds types not available in standard JSON: ObjectId (12-byte unique identifier), Date (64-bit UTC milliseconds), BinData (arbitrary binary blobs), Decimal128 (high-precision decimal), Int32, Int64, and Regular Expressions.
This type richness is why MongoDB queries can work with dates and object IDs natively, while a regular JSON API would need string representations and client-side parsing.