Overview
Introduction
BSON (Binary JSON) is the binary document format MongoDB stores data in, and it's occasionally useful to see real BSON bytes without spinning up a database.
This tool generates a random document and hand-encodes it into real BSON, showing both the raw bytes as a hex dump and a readable JSON preview of the same data.
What Is Random BSON Generator?
A random data generator whose output is a real BSON binary encoding, shown as a hex dump (since binary data isn't directly displayable as text) plus a JSON preview of the same document for readability.
The encoder is a small hand-rolled implementation following the official BSON spec's byte layout, not a wrapper around a database driver.
How Random BSON Generator Works
The tool builds a random object tree exactly like the JSON/XML/YAML/TOML generators in this category, then walks it, encoding each field according to its BSON type: strings get a length-prefixed UTF-8 encoding, integers that fit in 32 bits get the int32 type, other numbers get the double type, booleans get a single byte, null gets a type marker with no payload, arrays become embedded documents with numeric string keys ("0", "1", ...), and nested objects become embedded documents.
Each BSON document starts with a little-endian int32 declaring its own total byte length (including itself and the trailing null byte), followed by its encoded elements, followed by a single 0x00 terminator — the encoder builds this exactly per the spec.
The resulting bytes are rendered as a classic hexdump-style listing (offset, hex bytes, ASCII column), and the same source document is also serialized as indented JSON for a human-readable preview.
When To Use Random BSON Generator
Use it to get sample BSON bytes for testing a BSON parser, decoder, or byte-level inspection tool without needing a running MongoDB instance.
It's also useful for learning how BSON's binary layout works, since the JSON preview lets you connect the readable data to the specific bytes that encode it.
Often used alongside Random JSON Generator and Random XML Generator.
Features
Advantages
- A genuine hand-rolled encoder following the real BSON spec's type bytes and length-prefix rules, not a fabricated hex string.
- Shows both the raw hex dump and a readable JSON preview together, so the binary output is always interpretable.
- Shares the same random tree generator as the JSON/XML/YAML/TOML tools, for structural comparison across formats.
Limitations
- Only encodes double, string, embedded document, array, boolean, null, and int32 — no int64, date, binary, ObjectId, regex, JS code, or decimal128 types, which a full BSON library would support.
- The 32-bit-vs-double numeric split is a simplification: any integer that fits in a signed 32-bit range is always encoded as int32, and any other number as double, rather than offering int64 as a distinct choice.
Examples
Best Practices & Notes
Best Practices
- Cross-check the JSON preview against the hex dump if you're learning BSON's byte layout, rather than trying to read raw hex directly.
- Keep max depth modest (2-3) since deeply nested embedded documents produce long hex dumps that get tedious to inspect by hand.
Developer Notes
Numeric encoding chooses int32 vs. double based on Number.isInteger() and the signed 32-bit range, matching how many BSON producers handle plain JS numbers, though a spec-complete encoder would also need to support int64 (BSON type 0x12) for integers outside that range.
Random BSON Generator Use Cases
- Generating sample BSON bytes for testing a BSON decoder or parser
- Learning how BSON's binary layout (length prefixes, type bytes, cstring keys) works via a concrete example
- Producing a quick byte-level fixture without running MongoDB
Common Mistakes
- Assuming this encoder covers every BSON type MongoDB itself can produce — it's intentionally scoped to the types needed for this tool's random tree, not a full BSON implementation.
- Trying to read the hex dump as if it were plain text — it's binary data; the JSON preview is there specifically so you don't have to decode it by hand.
Tips
- Use a small max depth first to get a short, easy-to-follow hex dump before generating larger documents.
- Compare the JSON generator's output for the same settings to see the same kind of data in a human-readable text format.