DevTools Logo

BSON to JSON & Hex Decoder

BSON to JSON & Hex Decoder

Encode JSON as BSON, decode BSON bytes, and inspect a deterministic hex dump locally

Input

About

Encode and decode common BSON types entirely in your browser, including nested documents, arrays, int32, int64, doubles, booleans, null, and ObjectId values.

Examples

Spec hello-world vector

Input
bytesToHex(encodeBson({ hello: "world" }))
Output
"160000000268656C6C6F0006000000776F726C640000"

Matches the BSON specification's first example byte-for-byte (uppercase hex).

Int32 versus int64

Input
encodeBson({ a: 42 })[4] // 0x10; encodeBson({ a: 3000000000 })[4] // 0x12
Output
{ int32Length: 12, int64Length: 16 }

Safe-range integers use 4-byte int32; larger ones switch to 8-byte int64.

About this tool

BSON Hex Converter encodes JSON objects to BSON bytes with encodeBson and decodes them back with decodeBson: strings (0x02), nested documents (0x03), arrays (0x04), ObjectId (0x07 via { $oid }), booleans (0x08), null (0x0a), int32 (0x10), and int64 (0x12, with { $numberLong } fallback past safe-integer range) are supported, plus doubles (0x01). hexToBytes/bytesToHex bridge to hex text and hexDump renders offset-annotated inspection lines.

Type selection is automatic and spec-shaped: safe-range integers become int32, larger integers become int64, and the classic { hello: "world" } document encodes to the specification's 22-byte vector 160000000268656C6C6F0006000000776F726C640000. The BSON root must be a JSON object, keys cannot contain NUL, and non-finite numbers are rejected.

Errors use the BsonError class with explicit messages: odd-length or non-hex input, truncated cstrings and values, invalid document lengths or terminators, and trailing bytes after the root document all throw instead of returning partial data.

How to use this tool

  1. Encode a document

    Pass a JSON object to encodeBson — arrays must live under a key since the root cannot be a bare array.

  2. Read the hex

    Use bytesToHex for compact hex or hexDump for offset, hex, and ASCII columns when inspecting bytes.

  3. Decode bytes back

    Feed bytes (via hexToBytes for text) to decodeBson and get the round-tripped JSON object.

  4. Handle BsonError

    Catch BsonError for malformed hex, truncated buffers, bad ObjectIds, and unsupported BSON types.

Use cases

Debugging MongoDB wire bytes

Encode a document and compare its hex against captured traffic or driver output.

Verifying ObjectId payloads

Round-trip { $oid } values to confirm 24-hex-character identifiers survive encoding.

Teaching BSON layout

Use hexDump offsets to show length prefixes, type bytes, cstrings, and the trailing terminator.

Common mistakes

Mistake:Encoding a top-level array or scalar.

Fix:The BSON root must be a JSON object — wrap arrays as { list: [...] } before encoding.

Mistake:Comparing hex case-sensitively.

Fix:bytesToHex emits uppercase; lowercase expected vectors with .toLowerCase() on the actual value.

Mistake:Passing odd-length hex to hexToBytes.

Fix:Hex text needs an even digit count — a trailing half-byte throws BsonError.

Frequently asked questions

What does this tool do?

It converts between JSON objects and BSON bytes with encodeBson/decodeBson, bridges hex text via hexToBytes/bytesToHex, and renders offset-annotated inspections with hexDump — all errors surface as BsonError.

What does { hello: "world" } encode to?

The 22-byte specification vector 160000000268656C6C6F0006000000776F726C640000 (lowercase): 4-byte length, 0x02 string element, and trailing terminator. Note bytesToHex emits uppercase hex.

Is my document uploaded or stored?

No. Encoding, decoding, and hex rendering run entirely in your browser.

Why did I get a BsonError?

The root must be a JSON object (no bare arrays), keys cannot contain NUL, hex needs an even digit count, and truncated buffers, bad ObjectIds, or trailing bytes after the document all throw.

References & standards

.env Secret Safety Linter

Validate .env syntax and spot likely leaked credentials or placeholder secrets.

.htaccess Generator

Build an Apache .htaccess — HTTPS redirects, rewrites, error pages, security headers, and basic auth

2D Fluid Dynamics & Particle Physics Sandbox

A physics playground with two engines sharing one canvas. The fluid mode runs a real Eulerian Navier-Stokes solver — semi-Lagrangian advection, explicit diffusion, Jacobi pressure projection and vorticity confinement — that you paint with dye and velocity using mouse or touch. The particle mode is a damped Verlet N-body engine with gravity wells, Coulomb attractors and repulsors, spring links, wall restitution and elastic circle collisions. Particle counts adapt to the device (100k target on WebGL2 desktop, 2k-5k on CPU fallback), and every configuration exports as runnable code: a Matter.js world, a PixiJS ticker setup, or a standalone vanilla Canvas engine.

AI Function Calling Schema Generator

Generate function calling JSON schemas from TypeScript interfaces

Algorithm Arena & Data Structure Visualizer

Interactive arena for algorithms and data structures: eight sorting algorithms racing on one seeded input, six pathfinding algorithms walking generated mazes, and BST, AVL, Red-Black, B-tree, heap and trie operations replayed step by step with rotations highlighted. Every run shows the Big-O table and the phase-highlighted reference implementation in four languages, and compared values can be played as pitch (sonification) through the shared audio engine. Everything runs client-side; inputs never leave the browser.

Ansible Playbook Generator

Build an Ansible playbook — hosts, vars, and tasks with modules — as YAML