Convert JSON to Bson
Real BSON encoding per bsonspec.org - int32/int64/double types, embedded docs, arrays. Bidirectional. Free, offline, client-side.
Real BSON encoder/decoder per bsonspec.org - proper type tags, little-endian length prefixes, UTF-8 strings. Encodes the JSON types JavaScript can express (string / number / boolean / null / object / array) using BSON's int32, int64, double, bool, null, string, document, and array tags. Recognizes EJSON v2 tags ($date, $oid, $numberLong) on encode and emits them on decode.
How to Use Convert JSON to Bson
- Pick a mode - Encode (JSON → BSON bytes) or Decode (BSON bytes → JSON). Mode switch auto-re-runs if you have input loaded.
- Pick output format (encode) - Hex (compact) is the standard for inspection; Hex (with spaces) for human reading; Base64 for transmission; JS Uint8Array literal for paste-into-code.
- Paste JSON (encode mode) - must be a top-level object or array (BSON's document type). Numbers get classified: integers in int32 range → 0x10 int32; larger integers → 0x12 int64; non-integers → 0x01 double. Strings, booleans, null, nested objects, and arrays all map to standard BSON tags.
- Paste BSON bytes (decode mode) - accepts hex (with or without spaces), Base64, or a Uint8Array literal. Auto-detection picks the right parser. The output is JSON (pretty-printed by default).
- Toggle EJSON canonical (decode only) - when on, types that don't fit JSON (int64 outside the safe range, dates, doubles like Infinity) are emitted as MongoDB Extended JSON v2 canonical tags (
{"$numberLong":"9999999999"},{"$date":"..."}) instead of being lossily downcast. - Read the stats line - input vs output bytes, overhead percentage, top-level type (object/array), key count. BSON is rarely smaller than minified JSON for simple documents - it carries length prefixes and type tags.
- Copy or download - Copy puts the formatted output on your clipboard. Download saves: in encode mode the raw
.bsonbytes (compatible withbsondumpand MongoDB drivers), in decode mode a.jsonfile.
Frequently Asked Questions
Is this real BSON?
Yes. We implement the bsonspec.org binary format: little-endian length prefix, type byte + cstring key + value bytes for each field, document terminator 0x00. The hex output you’ll see for {"a":1} is 0C 00 00 00 10 61 00 01 00 00 00 00 – same bytes that MongoDB’s mongoexport --type=bson would produce.
Which BSON types are supported?
The standard ones JSON can express: 0x01 double, 0x02 string, 0x03 document, 0x04 array, 0x08 boolean, 0x0A null, 0x10 int32, 0x12 int64. Plus 0x09 UTC datetime via the EJSON $date tag. Not yet supported: $oid (ObjectId), $binary, $regex, $timestamp, $numberDecimal. Add EJSON tags to your JSON for the supported ones; the encoder will emit the correct BSON type.
How are numbers classified?
By examining the JavaScript value. If it’s a whole number in [-2^31, 2^31) → 0x10 int32. If it’s a whole number outside that but inside JS’s safe-integer range → 0x12 int64. Otherwise (non-integer, NaN, Infinity, very large) → 0x01 double. To force int64 explicitly, wrap with {"$numberLong":"9999999999"}.
What’s EJSON canonical and when do I want it?
MongoDB Extended JSON v2 – a JSON encoding that preserves BSON types when their values don’t fit natively. A BSON int64 of 9,999,999,999 round-trips through plain JSON as just 9999999999 (loses the int64 tag); through EJSON canonical it round-trips as {"$numberLong":"9999999999"}. Turn it on when you need lossless round-trips for typed pipelines.
Why is the BSON bigger than my JSON?
Each field carries a 1-byte type tag, the key as a null-terminated cstring, and a length prefix where applicable. For {"a":1} minified JSON is 7 chars; BSON is 12 bytes. BSON optimizes for parsing speed and rich types, not size. For wire compression use gzip on top.
Can I import the downloaded .bson into MongoDB?
Yes – the encode-mode “Download” button writes raw BSON bytes. You can stream them into mongoimport --type=bson or read with the official drivers (BSON.deserialize in node, bson.decode in Python). Round-trip with our decode mode is exact.
What if my JSON has a key with a NUL byte?
BSON uses cstrings (null-terminated) for field names, so they cannot contain x00. The encoder throws a clear error if it finds one. This is a hard limit of the spec – there’s no escaping mechanism inside a cstring.
How do I handle dates?
JSON has no Date type. Wrap your value in {"$date":"2026-01-01T00:00:00.000Z"} and the encoder emits a BSON 0x09 UTC datetime (8-byte int64 milliseconds since epoch). On decode with EJSON canonical on, dates come back as {"$date":"..."}; with it off, they round-trip as an ISO 8601 string.
Is my data uploaded?
No. All BSON encoding, decoding, and byte formatting runs in your browser via Uint8Array/DataView/BigInt arithmetic. Open DevTools → Network and watch zero requests fire after the page loads. Safe for proprietary schemas, audit logs, or anything sensitive.
Does it work offline?
Yes. Total bundle is under 24 KB (HTML + CSS + JS, no libraries). Once the page loads, disconnect and keep encoding/decoding. Useful for working with MongoDB exports on air-gapped systems.