skip to content

BSON vs JSON Trade-offs

BSON buys typing and fast traversal; it costs readability and, often, size, because length prefixes and type bytes can exceed what JSON spends on text. Binary is not automatically smaller.

on this pageshow

explore

questions

5

Why does BSON exist when the equivalent JSON is often smaller, and what does it buy in exchange?

level: juniorimportance: must knowfreq 46%

answer

  1. not a compression format
  2. three stated design goals
  3. extra lengths, fixed-width numbers
  4. types JSON has no word for
  5. readability is the price

basics

~20 s

BSON trades some size for faster traversal, cheap number decoding and richer types. Length prefixes and fixed-width numbers let a reader skip values and avoid text conversion. JSON stays readable, universal and, on many documents, smaller.

solid answer

~40 s

BSON is a binary encoding of JSON-like documents, and its goal was never to be the smallest form. Its own FAQ says that in some cases it uses more space than JSON, because it adds lengths of strings and subobjects to make traversal faster, and stores integers as 32 or 64 bits so they need no text parsing. `{"hello":"world"}` is 17 bytes of compact JSON and 22 of BSON. In exchange a reader can hop over values it does not need, and the format has dates, binary data and distinct numeric types that plain JSON lacks. The price is readability and reach: you need a library or a tool such as `bsondump` to see it. So JSON suits what people and unknown clients read, and BSON suits data that programs store and revisit.

code

plaintext · 11 lines
plaintext
{"hello":"world"} as BSON, 22 bytes in total

offset  bytes                bytes  meaning
0-3     16 00 00 00          4      int32 total size, 0x16 = 22, counts itself and the final NUL
4       02                   1      element type: UTF-8 string
5-10    68 65 6c 6c 6f 00    6      e_name cstring "hello" + NUL
11-14   06 00 00 00          4      string length 6: "world" + NUL, does not count these 4 bytes
15-20   77 6f 72 6c 64 00    6      "world" + NUL
21      00                   1      end of the document

4 + 1 + 6 + 4 + 6 + 1 = 22. The same document as compact JSON is 17 bytes.

go deeper

for a junior

Recall the trade in one breath: BSON spends some bytes on lengths and fixed-width values to gain traversal, faster numbers and extra types, and gives up readability.

for a middle

Be able to count a small document on both sides, naming the size prefix, type byte, key NUL and string length that make 22 bytes against 17.

for a senior

Show you choose per boundary rather than per preference: JSON where clients are unknown, BSON where a store reads fields repeatedly, and an explicit type mapping where they meet.

for a principal

Treat the format as a standing interface decision: what the organisation exposes, what it stores, and who owns the conversion rules between the two when they differ.

## The short version **BSON** is a binary format that stores zero or more ordered key/value pairs as one entity called a document. The name sounds like "compressed JSON", and that is the misreading to correct first. bsonspec.org lists three design goals: **lightweight**, **traversable** and **efficient**. Size is only one of them, and the site's FAQ states plainly that in some cases BSON uses even more space than JSON. It then gives the reason: the extra information exists to make traversal faster. **JSON**, by contrast, is text. A person can read it, any language can parse it, and it spends nothing on lengths because the end of a value is found by reading up to it. ## The same document in both formats Take `{"hello":"world"}`, the example on the specification's own FAQ page, written as compact JSON with no spaces: | Part | Compact JSON | BSON | |---|---|---| | Document framing | `{` and `}`: 2 bytes | `int32` total size and trailing NUL: 5 bytes | | Field name | `"hello"`: 7 bytes | `hello` plus NUL: 6 bytes | | Between name and value | `:`: 1 byte | type byte `\x02`: 1 byte | | Value | `"world"`: 7 bytes | `int32` length, `world`, NUL: 10 bytes | | **Total** | **17 bytes** | **22 bytes** | BSON comes out five bytes larger here: framing adds three and the string value adds three, while the name saves one. That is a real cost, and it is what the speed is paid with. ## What the extra bytes buy - **Traversal.** A string or embedded document carries its length, so a reader looking for one field can jump over a value without reading it. - **Numbers without text conversion.** An `int32` is four little-endian bytes. There are no digits to convert, which the FAQ gives as the reason integers are much faster to parse, while admitting that this uses more space than JSON for small integers. - **Types.** bsonspec.org names `Date` and `BinData` as additions JSON lacks. BSON also tells `int32`, `int64`, `double` and `decimal128` apart, where JSON has a single number syntax. - **No escaping of values.** String values are stored as UTF-8 with a length, so a quote or a newline inside one costs one byte rather than a two-byte escape. ## What it costs - **Readability.** You cannot usefully open BSON in a text editor. The `bsondump` tool exists to convert `.bson` files into a readable form. - **Reach.** A browser parses JSON natively; for BSON it needs a library. That matters at any boundary where you do not control the client. - **Size, on some shapes.** Short documents of small numbers and arrays grow, while dates, binary values and very long numbers shrink. "Binary" alone predicts neither. - **Not quite a superset.** A key is a `cstring`, which must not contain a NUL byte, so a JSON key containing `\u0000` has no BSON form. ## When each one is preferred | Situation | Usually preferred | Why | |---|---|---| | Public API, browsers, clients you do not control | JSON | Needs no special library, and people can read it | | Configuration and logs that people read | JSON | Inspection and hand-editing matter most | | Documents a database stores and reads field by field | BSON | Traversal and real types pay off on every read | | Traffic between your own services that needs dates or binary | Either, by test | BSON avoids string conventions, JSON avoids a dependency | The two also coexist. A common shape is an API that speaks JSON with a store that holds BSON, converting at the boundary. That boundary is exactly where types can get lost, so it deserves a deliberate mapping. One point of ownership is worth getting right: the BSON specification defines the types and the layout. A limit such as MongoDB's maximum document size belongs to the server, not to the format. ## How to answer in an interview 1. State the trade: BSON spends bytes on lengths and fixed-width values to gain traversal, cheap numbers and types. 2. Give one worked count, such as 17 against 22 bytes, to show that "binary is smaller" is false as a flat claim. 3. Name the cost, readability and reach, and say where each format belongs.

  • Can a BSON document ever be smaller than its JSON?
    Yes, when a value's text is longer than its binary form. `{"d":"2026-11-03T12:00:00.000Z"}` is 32 bytes of JSON, and the same instant as a BSON datetime is 16 bytes, because the value is eight bytes rather than 26 characters. `{"n":1234567890}` is 16 bytes of JSON and 12 of BSON. Short documents of small numbers go the other way.
  • Can every JSON document be converted to BSON without loss?
    Not quite. A BSON key is a `cstring`, which must not contain a NUL byte, so a JSON key holding `\u0000` cannot be represented. JSON's number syntax also has no size limit, while BSON's numbers are fixed width, so an integer beyond `int64` must become a `double` or `decimal128`, or be rejected. Ordinary documents convert without trouble.
  • If a service stores BSON, should its public API also speak BSON?
    Usually not. A public API serves clients you do not control, and JSON needs no extra library and can be read in a browser's network panel. Keep JSON at the boundary and BSON behind it, and write down how dates, identifiers and large numbers map between them, since plain JSON has no types for those and the conversion is where they get lost.

  • BSON is binary, so it is always smaller than the same JSON.
  • BSON exists mainly to compress JSON documents for storage.
  • BSON and JSON hold the same set of types, just encoded differently.
  • A BSON file can be read and edited in a text editor just like JSON.
  • The BSON specification sets the 16 MB maximum document size.
open as a page

A team moves JSON documents to BSON expecting to save space: what do they measure on short numeric-heavy and long string-heavy documents, and why?

level: middleimportance: should knowfreq 38%

basics

~20 s

Expect little saving. Documents of small numbers grow a few bytes, arrays of small numbers can grow several times over, and long plain strings stay within about a percent. BSON wins on long numbers, dates, binary and escape-heavy text.

open as a page

In BSON versus JSON, what work is saved when reading or changing one value in a large document, and what stays linear?

level: middleimportance: should knowfreq 31%

basics

~20 s

A BSON reader hops over values using type bytes and length prefixes, where a JSON parser scans all the text before the target. BSON still walks elements in order, with no field index, so lookup stays linear in the elements passed.

open as a page

A pipeline converts BSON documents to plain JSON and back before reloading them: which values come back changed, and why?

level: seniorimportance: should knowfreq 28%

basics

~20 s

Values plain JSON has no type for change: dates, ObjectIds and binary data return as strings or bare numbers, numeric types collapse into one syntax, large int64 values can lose precision, and NaN has no JSON form.

open as a page

Should a team set a fleet-wide rule shortening BSON field names to save space, and how would you decide?

level: principalimportance: nice to knowfreq 22%

basics

~20 s

Usually not as a blanket rule. Names sit in every document, but block compression can reclaim much on disk and cryptic names cost every reader. Measure their share of document size, and shorten only where small documents meet huge volume.

open as a page