JSON to BSON Converter

Encodes a JSON object into BSON (Binary JSON), the binary-encoded document format used by MongoDB's wire protocol and drivers, with a hex preview and a downloadable .bson file. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-22

Overview

Introduction

MongoDB stores and transmits documents as BSON, not JSON, so understanding or generating a valid BSON byte layout by hand for testing a driver, debugging a wire-protocol issue, or building a fixture is normally something you'd need a script for.

This tool encodes a JSON object into spec-correct BSON bytes directly in the browser, with both a hex preview and a downloadable binary file.

What Is JSON to BSON Converter?

A BSON encoder that converts a JSON object into BSON's binary document format: a length-prefixed sequence of typed elements (string, double, int32, int64, boolean, null, embedded document, and array), terminated by a null byte, per the official BSON specification.

The output is shown both as a hex dump for quick inspection and as a real binary file you can download and use directly.

How JSON to BSON Converter Works

The tool recursively encodes each JSON value according to its type: strings become BSON's length-prefixed UTF-8 string type, integers become int32 or int64 depending on magnitude, non-integer numbers become doubles, and nested objects and arrays become embedded BSON documents (with array indices used as element names).

Every document, top-level and embedded, is wrapped with its own 4-byte little-endian length prefix followed by its elements and a trailing null byte, exactly matching the structure MongoDB's own encoders produce.

When To Use JSON to BSON Converter

Use it when testing a MongoDB driver, wire-protocol implementation, or BSON parser and you need a known-correct BSON byte sequence generated from a specific JSON document.

It's also useful for learning how BSON's binary layout works by comparing a JSON document against its encoded byte structure directly.

Features

Advantages

  • Produces spec-correct BSON bytes without needing a MongoDB driver or server.
  • Offers both a hex preview for inspection and a real binary file for direct use.
  • Runs entirely client-side, so document content never leaves your browser.

Limitations

  • Only the common BSON types are supported (string, number, boolean, null, document, array); MongoDB-specific types like ObjectId, Date, and Binary aren't generated automatically from plain JSON.
  • Requires a top-level JSON object; arrays and scalars aren't valid top-level BSON documents.

Examples

Encoding a small document

Input

{"_id":1,"name":"Ada Lovelace","active":true}

Output

1e 00 00 00 10 5f 69 64 00 01 00 00 00 02 6e 61 6d 65 00 0c 00 00 00 41 64 61 20 4c 6f 76 65 6c 61 63 65 00 08 61 63 74 69 76 65 00 01 00

The 4-byte total length prefixes the document, followed by each typed element (int32 _id, string name, boolean active) and a trailing null terminator byte.

Best Practices & Notes

Best Practices

  • Use the hex preview to spot-check individual byte values against the BSON spec when learning the format.
  • Download the .bson file directly rather than trying to reconstruct binary data from the hex text by hand.
  • Remember that MongoDB-specific types (ObjectId, Date) aren't inferred from plain JSON; post-process the encoded bytes if you need those exact types.

Developer Notes

The encoder writes directly to a growable byte buffer using DataView for correct little-endian multi-byte values (BSON specifies little-endian throughout), and arrays are encoded by reusing the same embedded-document logic as objects, with numeric string keys ("0", "1", ...) standing in for array indices, exactly how the BSON spec defines array encoding.

JSON to BSON Converter Use Cases

  • Generating a known-correct BSON fixture for testing a MongoDB driver or parser
  • Learning BSON's binary layout by comparing it against the equivalent JSON
  • Debugging a wire-protocol issue that requires inspecting raw BSON bytes

Common Mistakes

  • Passing a top-level JSON array, which BSON doesn't support as a document root.
  • Expecting MongoDB-specific types like ObjectId or Date to be generated automatically from plain JSON values.
  • Trying to reconstruct binary data by manually copying the hex text instead of using the direct file download.

Tips

  • Cross-check small examples against the official BSON spec's byte layout while learning the format.
  • Use the downloaded .bson file directly with a hex editor or driver test harness rather than the hex text for anything beyond visual inspection.

References

Frequently Asked Questions