You might have heard about JSON(JavaScript Object Notation) but have you heard about BSON?
And no it's not pronounced as Bison.
There are a few key differences between JSON and BSON(Binary JSON).
JSON - the predecessor of BSON🧬
To understand what BSON is and why it was developed, we first need to know about the origin of JSON.
JSON, or JavaScript Object Notation, is a human-readable data interchange format, specified in the early 2000s. Even though JSON is based on a subset of the JavaScript programming language standard, it’s completely language-independent.
The following shows an example :
{
"_id": 1,
"name": { "first" : "John", "last" : "Backus" },
"contribs": [ "Fortran", "ALGOL", "Backus-Naur Form", "FP" ],
"awards": [
{
"award": "W.W. McDowell Award",
"year": 1967,
"by": "IEEE Computer Society"
}, {
"award": "Draper Prize",
"year": 1993,
"by": "National Academy of Engineering"
}
]
}
Now, to provide flexibility with storage the MongoDB team wanted to use the JSON format to store data.
But there were some issues with using JSON as a data storage format.
Issues with JSON
📌 JSON only supports a limited number of basic data types. Most notably, JSON lacks support for dates and binary data.
📌 JSON objects and properties don’t have fixed length which makes traversal slower.
In order to make MongoDB JSON-first, but still high performance and general purpose, BSON was invented to bridge the gap: a binary representation to store data in JSON format, optimized for speed, space, and efficiency.
What is BSON(anyways)?
When you think of BSON, remember it is not something entirely different from JSON. It's just how EVs are better than regular fuel gas cars(in some ways).
🚨 No, BSON is not a scam. It provides some power ups 💪 over JSON that are particularly useful for storage. But for data transfers over the network, JSON is still the No. 1 choice.
BSON stands for “Binary JSON,” and that’s exactly what it was invented to be.
👉 BSON’s binary structure encodes type and length information, which allows it to be traversed much more quickly compared to JSON.
👉 BSON adds some non-JSON-native data types, like dates and binary data, without which MongoDB would have been missing some valuable support.
Let's see 2 examples where JSON data is converted to BSON during storage and how it looks :
{"hello": "world"} →
\x16\x00\x00\x00 // total document size
\x02 // 0x02 = type String
hello\x00 // field name
\x06\x00\x00\x00world\x00 // field value
\x00 // 0x00 = type EOO ('end of object')
{"BSON": ["awesome", 5.05, 1986]} →
\x31\x00\x00\x00
\x04BSON\x00
\x26\x00\x00\x00
\x02\x30\x00\x08\x00\x00\x00awesome\x00
\x01\x31\x00\x33\x33\x33\x33\x33\x33\x14\x40
\x10\x32\x00\xc2\x07\x00\x00
\x00
\x00
You can learn more about BSON grammar here 👉: BSON Specifications
JSON vs BSON
JSON and BSON are basically cousins 👫 that share some features but not all as you can see above.
One particular way that differentiates BSON from JSON is that BSON provide better Data type support. JSON only has the number data type but BSON provides support to differentiate integers from floating point numbers. 😎😎
This is a great advantage when it comes to data storage and operations. The functions working on integers can provide different output from floating point.
Top comments (0)