From JSON to BSON: The Data Format MongoDB Actually Uses



This content originally appeared on DEV Community and was authored by Rijul Rajesh

If you’ve worked with MongoDB or other document-based databases, you may have come across BSON. It sounds similar to JSON, and it is — but with some important differences under the hood.

JSON: The Familiar Friend

JSON (JavaScript Object Notation) is a lightweight, text-based format for storing and exchanging data. It’s readable, widely used, and supported by almost every programming language.

Here’s a typical JSON object:

{
  "name": "Alice",
  "age": 28,
  "isMember": true
}

It’s simple, readable, and works great for APIs, configs, and data interchange.

BSON: The Binary Upgrade

BSON stands for Binary JSON, and it’s the internal data format used by MongoDB.

Think of BSON as JSON’s binary cousin — optimized for storage and speed. It maintains the same basic structure (documents made of key-value pairs) but adds a few superpowers.

Here’s what sets BSON apart:

1. Binary Format

Unlike JSON, BSON is not plain text — it’s a binary-encoded format. This means:

  • It’s faster to parse for machines.
  • It uses less space (in some cases).
  • It’s not human-readable without decoding.

2. More Data Types

JSON only supports basic types: string, number, boolean, null, array, and object.

BSON supports those plus:

  • Date
  • int32, int64
  • Decimal128
  • Binary (for raw bytes)
  • ObjectId (used as MongoDB’s default _id)
  • Timestamp and Regex

Example: In JSON, dates are just strings:

{ "createdAt": "2025-07-15T10:00:00Z" }

In BSON, dates are stored as actual date objects, which MongoDB can sort or query more efficiently.

Why MongoDB Uses BSON

MongoDB chose BSON over JSON for a few key reasons:

Efficient Storage

BSON allows compact binary encoding and supports size-prefixed types, which help MongoDB scan and retrieve fields quickly.

Richer Queries

Having native support for types like Date and ObjectId makes it easier to write type-aware queries (like range queries on dates) without extra parsing.

Fast Serialization

BSON can be encoded and decoded faster than JSON in many cases, especially when handling large datasets or binary blobs.

Tradeoffs to Know

While BSON is great for databases, it’s not always ideal for other use cases.

Feature JSON BSON
Format Human-readable text Binary
Readable Yes No (needs a decoder)
Data Types Basic only Extended (date, binary, etc.)
File Size Usually smaller for simple data Larger in some nested structures
Usage APIs, configs, web MongoDB internal, data storage

So, for web APIs or config files, JSON remains the go-to. But for database internals where performance and type-awareness matter, BSON shines.

If you’re using MongoDB drivers in Python, Node.js, or any other language, the driver takes care of translating between JSON-like documents and BSON under the hood.

Wrapping Up

  • JSON is great for human-readable data interchange.
  • BSON is optimized for machines — it’s faster, richer in data types, and ideal for MongoDB’s internal storage.

If you’re a software developer who enjoys exploring different technologies and techniques like this one, check out LiveAPI. It’s a super-convenient tool that lets you generate interactive API docs instantly.

LiveAPI helps you discover, understand and use APIs in large tech infrastructures with ease!

So, if you’re working with a codebase that lacks documentation, just use LiveAPI to generate it and save time!

You can instantly try it out here! 🚀


This content originally appeared on DEV Community and was authored by Rijul Rajesh