Convert JSON to BSON

Convert JSON data structure to BSON (Binary JSON) format.

Input (JSON)
Options
BSON Extension
File extension (when downloading).
Output (BSON)

What It Does

Convert JSON data to BSON (Binary JSON) format and explore exactly how MongoDB stores your documents under the hood. BSON is the binary-encoded serialization format that MongoDB uses internally to store and transfer data — and while it shares structural similarities with JSON, the two formats have meaningful differences in type richness, storage size, and traversal performance that every database developer should understand. This tool takes any valid JSON input and produces a detailed BSON representation, annotating each value with its corresponding BSON type code. You can immediately see how a JavaScript number becomes a BSON Double or Int32, how a date string maps to a BSON Date object, and how nested documents and arrays are encoded. Side-by-side size comparisons let you gauge whether your documents will be larger or smaller after BSON serialization — a practical consideration when designing schemas for high-volume MongoDB collections. Whether you're a backend developer integrating a Node.js application with MongoDB, a data engineer migrating relational data into a document store, or a student learning NoSQL concepts, this tool gives you a transparent view into BSON encoding without needing a running MongoDB instance. Use it to validate type mappings, anticipate storage overhead, and catch data type mismatches before they become runtime bugs in production.

How It Works

The Convert JSON to BSON applies its selected transformation logic to your input and produces output based on the options you choose.

It applies a fixed set of transformation rules to your input, so the output is stable and easy to verify.

All processing happens in your browser, so your input stays on your device during the transformation.

Common Use Cases

  • Previewing exactly how a JSON document will be stored in a MongoDB collection before writing application code.
  • Verifying BSON type mappings when migrating data from a relational database to MongoDB to ensure numeric and date fields are encoded correctly.
  • Estimating document storage size in MongoDB during schema design to avoid hitting the 16 MB BSON document size limit.
  • Debugging unexpected data type behavior in MongoDB queries, such as a field being stored as Double instead of Int32.
  • Learning BSON encoding as part of a MongoDB certification or database development course.
  • Auditing third-party JSON payloads to understand how they will be deserialized by a MongoDB driver.
  • Comparing the storage efficiency of different JSON structures to choose the most compact schema for a high-throughput collection.

How to Use

  1. Paste or type your valid JSON object or array into the input panel on the left side of the tool.
  2. The tool will instantly parse your JSON and display the BSON representation with each field annotated by its BSON type code (e.g., Double, String, ObjectId, Date, Boolean, Array, Document).
  3. Review the type annotations carefully — pay close attention to numeric fields, which may be mapped to Double, Int32, or Int64 depending on their value range.
  4. Check the size comparison panel to see the byte size of your original JSON versus the encoded BSON output, helping you understand the storage overhead implications.
  5. If any field is not mapping to the BSON type you expect, adjust your JSON — for example, use an ISO 8601 date string to ensure proper Date type detection.
  6. Copy the annotated BSON structure or export the results to use as reference documentation for your MongoDB schema design.

Features

  • Automatic BSON type annotation for every field, including nested documents and array elements, so you know exactly how MongoDB will interpret your data.
  • Side-by-side byte size comparison between the raw JSON input and the BSON-encoded output to help you plan storage budgets.
  • Support for the full range of BSON data types including Double, String, Document, Array, Binary, ObjectId, Boolean, Date, Null, Int32, Int64, and Decimal128.
  • Real-time conversion with instant feedback as you type or paste JSON, eliminating the need to manually install BSON libraries to inspect encodings.
  • Clear visual differentiation between BSON type codes and values, making it easy to scan complex, deeply nested documents at a glance.
  • Handles edge cases such as JavaScript's maximum safe integer boundaries, correctly distinguishing between Int32 and Int64 representations.
  • No server-side processing — your data is converted entirely in the browser, keeping sensitive document contents private.

Examples

Below is a representative input and output so you can see the transformation clearly.

Input
{"name":"Ada","score":9}
Output
BSON (hex): 16000000026e616d650004000000416461000973636f72650009000000

Edge Cases

  • Very large inputs may take a few seconds to process in the browser. If performance slows, split the input into smaller batches.
  • Mixed formatting (tabs, line breaks, or inconsistent delimiters) can affect output. Normalize spacing first if needed.
  • Convert JSON to BSON follows the selected options strictly. If the output looks unexpected, re-check option settings and input format.

Troubleshooting

  • Output looks unchanged: confirm the input contains the pattern this tool modifies and that the correct options are selected.
  • Output differs from a previous run: confirm that the input and every option match, because deterministic tools should repeat when the settings are identical.
  • Unexpected characters: check for hidden whitespace or encoding issues in the input and try normalizing first.
  • Slow processing: reduce input size or try a modern browser with more available memory.

Tips

When designing MongoDB schemas, prefer Int32 over Double for integer counters and IDs where possible — BSON Doubles use 8 bytes while Int32 uses only 4, which adds up significantly across millions of documents. If you're working with timestamps, always use a proper ISO 8601 date string (e.g., "2024-01-15T10:30:00Z") so the tool and MongoDB drivers map your value to the efficient BSON Date type rather than a plain String. Keep an eye on the total document size shown in the comparison panel — MongoDB enforces a hard 16 MB limit per document, and deeply nested structures with large arrays can approach that ceiling faster than you might expect.

## Understanding BSON: Why MongoDB Doesn't Just Use JSON At first glance, BSON — Binary JSON — might seem redundant. JSON is already a widely supported, human-readable data format, so why would MongoDB invent an entirely new binary encoding? The answer comes down to three core requirements that plain JSON simply cannot satisfy at scale: type richness, traversal speed, and encoding efficiency. ### What JSON Gets Wrong for a Database JSON represents numbers as a single "number" type with no distinction between integers and floating-point values. For a configuration file or a REST API response, that ambiguity is usually harmless. For a database handling financial records, scientific measurements, or distributed system identifiers, the difference between an Int32, an Int64, a Double, and a Decimal128 is critically important. BSON introduces explicit numeric types, preserving precision and enabling the database engine to apply the correct comparison and arithmetic operations. JSON also has no native representation for binary data, dates, regular expressions, or the ObjectId type that MongoDB uses as a default primary key. Representing a binary file in JSON requires Base64 encoding, which inflates the payload size by roughly 33% and adds encoding/decoding overhead. BSON handles binary data natively with a dedicated Binary subtype, keeping the payload compact and avoiding the encoding roundtrip. ### How BSON Encoding Works A BSON document is a byte sequence that starts with a four-byte little-endian integer declaring the total document length, followed by a list of element entries, and terminated by a null byte. Each element entry consists of a one-byte type code, a null-terminated key string, and a type-specific value encoding. This structure means a MongoDB server can skip to any field in a document without parsing the entire payload — a significant performance advantage for large documents with many fields. By contrast, a JSON parser must read every character from the start of the string to find a field near the end. For a database performing millions of field-level reads per second, this linear scan cost is unacceptable. BSON's length-prefixed structure enables the engine to jump directly to the byte offset of a target field. ### BSON vs. MessagePack and Other Binary Formats BSON is not the only binary document format. MessagePack is a compact binary serialization format that is often smaller than BSON and faster to encode. However, MessagePack does not include field names in its encoding by default and uses integer keys, making it harder to inspect and debug. BSON prioritizes debuggability and type expressiveness over raw compactness — a deliberate trade-off MongoDB made to keep document inspection straightforward for developers. Protocol Buffers (protobuf) by Google offer excellent performance and strict schemas but require a pre-defined schema file. BSON is schemaless by design, aligning with MongoDB's flexible document model where different documents in the same collection can have different fields. ### Practical Size Implications For small documents, BSON is often slightly *larger* than equivalent JSON because it stores explicit type codes and length prefixes. A simple `{"x": 1}` in JSON is 7 bytes; in BSON it is 12 bytes. However, as documents grow larger and contain binary data, dates, or large integers, BSON's native type support eliminates the encoding overhead that JSON workarounds would introduce. The break-even point depends heavily on document structure, which is exactly why a size comparison tool like this one is valuable during schema design.

Frequently Asked Questions

What is BSON and how is it different from JSON?

BSON stands for Binary JSON and is the binary-encoded serialization format used by MongoDB to store and transfer documents. While JSON is a text-based, human-readable format with a limited set of data types (string, number, boolean, array, object, null), BSON extends this with additional types such as Int32, Int64, Double, Decimal128, Date, ObjectId, Binary, and Regular Expression. BSON documents also include explicit length prefixes that allow the database engine to skip fields without parsing the entire document, resulting in faster read operations. The trade-off is that BSON is not human-readable without a decoder.

Why does MongoDB use BSON instead of plain JSON?

MongoDB uses BSON for three primary reasons: richer data types, faster traversal, and native binary support. Plain JSON represents all numbers as a single "number" type, which means a database cannot distinguish between an integer and a floating-point value without application-level logic. BSON provides explicit numeric types, preserving precision for financial and scientific data. Its length-prefixed structure also allows MongoDB to navigate to specific fields in a document without reading the entire byte sequence, significantly improving query performance on large documents.

What is the maximum size of a BSON document in MongoDB?

MongoDB enforces a maximum BSON document size of 16 megabytes. This limit exists to prevent any single document from consuming excessive memory during query execution and to encourage proper data modeling — very large documents are typically a sign that data should be split across multiple documents or a separate collection. If you need to store files larger than 16 MB, MongoDB recommends using GridFS, which splits large files into chunks and stores them as a sequence of BSON documents.

How does this tool convert JSON to BSON?

This tool parses your JSON input in the browser and maps each value to its most appropriate BSON type using the same rules MongoDB drivers follow. For example, integer values within the 32-bit signed range are mapped to Int32, larger integers to Int64, decimal numbers to Double, and ISO 8601 date strings to BSON Date objects. The tool then displays the annotated structure alongside a byte-size comparison between the raw JSON and the BSON-encoded output. No data is sent to a server — all processing happens client-side.

Will a JSON number always become a BSON Double?

Not necessarily — it depends on the MongoDB driver and the value itself. The official BSON specification defines multiple numeric types: Double (64-bit IEEE 754 floating point), Int32 (32-bit signed integer), Int64 (64-bit signed integer), and Decimal128 (128-bit decimal floating point for high-precision arithmetic). Many drivers, including the official Node.js driver, default to Double for JSON numbers unless you explicitly use typed constructors like `new Int32(42)`. This tool mirrors that behavior and shows you the default type mapping so you can make an informed decision about whether to enforce stricter types in your application code.

Is BSON always smaller than JSON in terms of storage size?

No — for small, text-heavy documents, BSON is often slightly larger than the equivalent JSON because each BSON element includes a one-byte type code and a null-terminated key string in addition to the value. A simple document like `{"name": "Alice"}` takes fewer bytes as JSON text than as a BSON-encoded document. However, for documents containing binary data, dates, or large integers, BSON's native type encoding eliminates the overhead of JSON workarounds like Base64, and the overall payload can be more compact. The size comparison panel in this tool shows you the exact difference for your specific document.