JSON vs BSON
JSON vs BSON — Quick Summary
| JSON | BSON | |
|---|---|---|
| Full name | JavaScript Object Notation | Binary JSON |
| Encoding | Text (UTF-8) | Binary |
| Human readable | Yes | No — requires a decoder to inspect |
| Data types | string, number, boolean, null, object, array | Adds: Int32, Int64, Double, Date, Binary, ObjectId, Decimal128, Regex |
| Size | Smaller for simple data — no type-tag overhead | Larger per-field (type byte + length-prefixed strings) but faster to traverse |
| Parse speed | Requires tokenizing text | Faster to scan — lengths are pre-computed, no string parsing needed |
| Primary use | APIs, config, logs, browser storage | MongoDB's internal storage and wire format |
| Native date type | No — dates are ISO strings by convention | Yes — a real Date/timestamp type |
| Editable by hand | Yes, in any text editor | No — needs a tool to decode the binary |
| Best for | Data interchange between systems and browsers | Database internals where traversal speed and rich types matter |
Side-by-Side Example
The same document — as JSON text, and as MongoDB stores it (BSON, shown here decoded to its typed form):
json
{
"_id": "65a1f3c2e4b0a1b2c3d4e5f6",
"name": "Ravi Kumar",
"signupDate": "2024-01-15T10:30:00Z",
"balance": 1024.5,
"active": true
}text
// BSON — decoded view of the binary wire format
{
_id: ObjectId("65a1f3c2e4b0a1b2c3d4e5f6"), // 12-byte binary, not a string
name: "Ravi Kumar",
signupDate: ISODate("2024-01-15T10:30:00Z"), // real Date type, not a string
balance: 1024.5, // Double, type-tagged
active: true
}
// Every field is prefixed with a type byte and (for strings) a length —
// no text parsing needed to skip or seek within the document.JSON's "_id" and "signupDate" are plain strings; BSON stores them as a real ObjectId and Date, which is why MongoDB queries can range-filter dates and IDs without parsing text.
Parsing in Go
go
// JSON — standard library
import "encoding/json"
var doc map[string]any
err := json.Unmarshal(jsonBytes, &doc)
// BSON — via the official MongoDB driver
import "go.mongodb.org/mongo-driver/bson"
var doc2 bson.M
err = bson.Unmarshal(bsonBytes, &doc2)
// Documents almost always arrive as BSON only when read directly from
// MongoDB's wire protocol; the driver decodes it to native Go types for you.When JSON Beats BSON
- Anything that isn't MongoDB's wire protocol — REST APIs, config files, logs, and browser storage all use JSON — BSON isn't a general-purpose interchange format.
- Human inspection and debugging — you can open JSON in any text editor; BSON needs a decoder (e.g. mongo shell, bsondump) to read.
- Smaller simple payloads — for small documents with mostly strings, JSON's lack of type-tag overhead can make it more compact.
When BSON Beats JSON
- MongoDB storage and queries — BSON is MongoDB's native format — every document you write to or read from MongoDB is BSON on the wire, decoded to JSON-like objects by the driver.
- Traversal speed — length-prefixed fields let the database skip over values without parsing them, which matters at the scale MongoDB operates at.
- Richer, precise types — a real Date, Int64, Decimal128, and ObjectId avoid the string-encoding ambiguity JSON has for dates and large numbers.