MessagePack, CBOR and BSON to JSON with a Hex View
Operator: Abdulla Al Fahad. How tools are built and checked
The nine bytes a2 61 61 01 61 62 82 02 03 are the CBOR for {"a": 1, "b": [2, 3]}. Paste them here, choose CBOR, MessagePack or BSON, and you get the JSON, a row for every value with its binary type, and a hex view that highlights the bytes behind whichever value you click.
Key features
- Decodes MessagePack, CBOR (RFC 8949) and BSON from hex, base64 or a file of up to 8 MB
- Binary, date, big-integer, extension and tag values written as small wrapper objects, so nothing is lost
- A type and byte range (start to end, end exclusive) for every value, with the bytes highlighted in a hex dump
- CBOR indefinite lengths, half-precision floats and tags; MessagePack timestamps; BSON decimal128 and multi-document files
- Encodes JSON back to all three formats, choosing the smallest integer sizes
- Relaxed or canonical MongoDB Extended JSON for BSON
How binary values are written into JSON
In MessagePack and CBOR output, binary data is {"$binary": "<base64>"}, a date is {"$date": "<ISO 8601>"}, an integer beyond the 53 bits a JavaScript number holds exactly is {"$bigint": "<digits>"}, NaN and the infinities are {"$float": "NaN"}, a MessagePack extension is {"$ext": <type>, "hex": "..."}, and an unrecognised CBOR tag is {"$tag": <number>, "$value": ...}. The MessagePack timestamp extension (type -1, in its 4, 8 and 12-byte forms) and CBOR tags 0 and 1 become dates; tags 2 and 3 (bignums) become integers; tag 55799 is skipped as a self-describe marker.
A map key that is not a text string is written as its JSON text, with a warning, because JSON object keys must be strings. Duplicate keys are kept in their original order. The page reads up to 300,000 values nested no deeper than 100 levels, and a count that claims more items than there are bytes left is rejected before any memory is reserved.
BSON follows MongoDB Extended JSON: {"$oid"}, {"$date"}, {"$numberLong"}, {"$numberDecimal"}, {"$binary": {"base64", "subType"}}, {"$uuid"}, {"$regularExpression"}, {"$timestamp"}, {"$minKey"} and the rest. Relaxed mode writes int32 and ordinary doubles as plain numbers and dates as ISO strings; canonical mode wraps every number ({"$numberInt"}, {"$numberDouble"}) and writes dates as milliseconds. A file of several documents, as written by mongodump, is shown as an array.
Common use cases
- A developer encodes {"id": 1001, "name": "sensor-7", "ok": true, "ratio": 0.75, "samples": [1, -2, 300, 70000]} in all three: 59 bytes as MessagePack, 53 as CBOR and 94 as BSON. BSON is largest because it stores each array index as a key and a type byte per value; CBOR is smallest because 0.75 fits a 16-bit float, where MessagePack spends 9 bytes on a float64.
- A security engineer decodes the CBOR attestation object from a WebAuthn registration and sees its three keys, fmt, attStmt and authData, with authData as a binary value whose length is shown next to its bytes.
- A backend team opens a mongodump file with 12 documents, switches to canonical mode, and sees which fields are really int64 and which are doubles.
How to use this tool
- Pick MessagePack, CBOR or BSON, then paste hex or base64, or drop a file.
- Press Decode and read the JSON; warnings above it list anything that was approximated.
- Click a byte range in the table to see those bytes highlighted in the hex dump; 3-4 means the single byte at offset 3.
- Switch to the encode direction, paste JSON with the wrapper objects you need, and download the bytes.
Example
CBOR bytes pasted as hex
a2 61 61 01 61 62 82 02 03Decoded JSON and the first rows of the type table (byte ranges are start-end, end exclusive, counting from 0)
{"a": 1, "b": [2, 3]}
root map 2 keys bytes 0-9
a unsigned int 1 bytes 3-4
b array 2 items bytes 6-9Edge cases to watch
In BSON a bare whole number such as 5 is stored as int32, or int64 when it is larger. A field your driver keeps as a double needs a fraction in the JSON (5.0) or {"$numberDouble": "5"}, otherwise the type changes on the round trip.
CBOR dates decode to ISO 8601 text, so the difference between tag 0 (text) and tag 1 (epoch) is only visible in the type column; the encoder writes the form you choose in the dates selector.
Only the bytes you paste are decoded. Framing around them, such as a length prefix or a websocket header, shows up as an error at its first byte.
- Encode a value as {"$bigint": "18446744073709551615"} rather than a bare number: a plain JSON number that large would already have lost digits before it reached the encoder, which is why the parser reads integers as exact text.
- Decode, edit one value in the JSON and encode again with the same format: the encoder accepts every wrapper the decoder writes, so binary, date and big-integer values survive the edit.
Common errors and how to fix them
- "An array claims 2 items but only 1 bytes remain (at byte offset 1)"
- The data stops before the structure it announces, so it is truncated or has a prefix. Check the end of the copy and that nothing precedes the first type byte.
- "The document does not end with a zero byte (at byte offset 4)"
- A BSON document is its 4-byte length, its elements and a final zero. Either the length field is wrong or the file is not BSON; try MessagePack or CBOR.
- "18446744073709551616 is larger than a 64-bit unsigned integer, which MessagePack cannot hold"
- MessagePack stops at 64 bits. CBOR can store the same value as a bignum, so either choose CBOR or keep the number as a string.
Best practices
- Decode a payload you did not write with the format selected explicitly rather than guessing from the first byte: 0x82 starts a two-entry map in MessagePack and a two-item array in CBOR.
- When checking a producer, compare the type column against the schema: a counter written as a float64 in MessagePack, or a date written as a string, is a bug the JSON view alone hides.
Frequently asked questions
Why does a map key like 1 come out as "1"?
JSON keys can only be strings, so a numeric or other non-text key is written as its JSON text and a warning says so. Keys are never merged, so two keys that become the same text both stay.
Why does the MessagePack encoder always write floats as 64-bit?
A JSON number has no 32-bit form, and narrowing to float32 can change the value, so non-integers use float64. The CBOR encoder does shorten a float to 16 or 32 bits, but only when the value survives exactly.