BSON Hex Viewer

Paste a one-field JSON object to see its BSON bytes in hex.

How it is calculated

The document is laid out as int32 total length | type byte | key as C-string | value | 0x00. The total length is little-endian and counts itself, the element and the final terminator byte. A text value gets type 0x02 and is stored as an int32 length (text bytes plus one) followed by the UTF-8 bytes and a 00 terminator. A whole-number value gets type 0x10 and is stored as four little-endian bytes. Only a JSON object with exactly one field is accepted, and its value must be a string or an integer from -2147483648 to 2147483647. Anything else returns no result. Bytes are shown in uppercase hex separated by spaces, with no per-field annotation.

Worked example

For {"hello":"world"}: the key hello takes 5 bytes plus a 00 terminator (6). The value world is 5 bytes plus a terminator, so its length prefix is 06 00 00 00 and the value part takes 4 + 6 = 10 bytes. Add the type byte 02 (1 byte) and the 4-byte total length and the final 00 (1 byte): 4 + 1 + 6 + 10 + 1 = 22. The first four bytes are therefore 16 00 00 00 (hex 16 = 22), and the tool reports a total length of 22 bytes, field type string (0x02).

Try this example →

Common mistakes

  • Pasting a document with two or more fields, a nested object or an array. The viewer encodes exactly one field, so these give no result.
  • Using booleans, decimals such as 1.5, null or integers above 2147483647. Only strings and 32-bit integers are supported, and a large number would need int64 (0x12), which is not offered here.
  • Counting characters instead of bytes. A non-ASCII character like é takes two UTF-8 bytes, so the length prefix and total length grow by more than the visible text suggests.
  • Reading the length bytes as big-endian. BSON stores integers little-endian, so 22 appears as 16 00 00 00, not 00 00 00 16.
  • Forgetting that the string length prefix includes the terminating zero byte, so it is one larger than the text length.

About this calculator

BSON (Binary JSON) is the binary-encoded serialization format MongoDB uses to store documents and exchange them over its wire protocol — it extends JSON with extra types like dates and binary data, and every element in the byte stream follows the same layout: a one-byte type code, a null-terminated field-name string, and then the value encoded according to its type (e.g., a 32-bit little-endian integer, or a 4-byte length prefix followed by UTF-8 bytes for a string).

This viewer takes a small JSON object with a single field (a string or a 32-bit integer value) and encodes it exactly as MongoDB's driver would, then displays the resulting bytes as hexadecimal together with the total document length and the field's type — the hex itself is not labeled, but you can read the length prefix, type byte, field name and value bytes off the layout above — so you can see how JSON's text representation maps onto BSON's compact binary layout without needing to run a database driver.

Was this helpful?

You can pick up to 3 reactions.

Comments (1)

  • Editorial team

    This viewer encodes a one-field object only, so use it to learn the byte layout rather than to inspect real database documents. The input {"n":5} gives 0C 00 00 00 10 6E 00 05 00 00 00 00, 12 bytes, with 10 marking a 32-bit integer and 6E being the letter n. Two fields return nothing. Which other BSON types would you want to see supported next?

Popular calculators

All Calculators