JSON vs BSON

JSON vs BSON — Quick Summary

JSONBSON
Full nameJavaScript Object NotationBinary JSON
EncodingText (UTF-8)Binary
Human readableYesNo — requires a decoder to inspect
Data typesstring, number, boolean, null, object, arrayAdds: Int32, Int64, Double, Date, Binary, ObjectId, Decimal128, Regex
SizeSmaller for simple data — no type-tag overheadLarger per-field (type byte + length-prefixed strings) but faster to traverse
Parse speedRequires tokenizing textFaster to scan — lengths are pre-computed, no string parsing needed
Primary useAPIs, config, logs, browser storageMongoDB's internal storage and wire format
Native date typeNo — dates are ISO strings by conventionYes — a real Date/timestamp type
Editable by handYes, in any text editorNo — needs a tool to decode the binary
Best forData interchange between systems and browsersDatabase 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 protocolREST APIs, config files, logs, and browser storage all use JSON — BSON isn't a general-purpose interchange format.
  • Human inspection and debuggingyou can open JSON in any text editor; BSON needs a decoder (e.g. mongo shell, bsondump) to read.
  • Smaller simple payloadsfor small documents with mostly strings, JSON's lack of type-tag overhead can make it more compact.

When BSON Beats JSON

  • MongoDB storage and queriesBSON 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 speedlength-prefixed fields let the database skip over values without parsing them, which matters at the scale MongoDB operates at.
  • Richer, precise typesa real Date, Int64, Decimal128, and ObjectId avoid the string-encoding ambiguity JSON has for dates and large numbers.

Frequently Asked Questions

Rarely. Application code reads and writes plain JSON-like objects; the driver handles BSON encoding/decoding transparently. BSON matters when you're debugging at the wire-protocol level or hitting type-precision edge cases (e.g. large integers, dates).

Not always — it depends on the data. BSON's per-field type byte and length prefixes add overhead for small documents, but it avoids the escaping overhead JSON has for certain strings, and it's usually faster to traverse either way.

Use JSONKit's JSON to BSON tool to see the encoded structure. In application code, you'd typically pass a plain object straight to your MongoDB driver, which handles the BSON conversion for you.

BSON adds types JSON lacks (native dates, distinct 32/64-bit integers, binary data, ObjectId) and its length-prefixed encoding lets MongoDB seek through documents without parsing text — both matter for a database engine at scale.

Related Tools