DEV Community

Cover image for JSON vs BSON
Abraham E. Tavarez
Abraham E. Tavarez

Posted on


What's the difference between JSON and BSON?

To better understand the difference between JSON and BSON, first let's make sure we understand what JSON is.


JSON is a lightweight data-format that is often used for structuring data primarily to send data between a server and an application over the internet.
Many developers prefer building APIs using JSON format because of many reasons like for example readability.

Let's look at some key details about JSON:

  • JSON stands for JavaScript Object Notation
  • JSON is based on JavaScript in that they are conceptually similar to objects in JavaScript.
  • JSON is really easy for people to read and write, and easy for computers to parse and generate.
  • JSON is a text format and is completely language independent.

Now let's look at an example representing a completely made up DEV post JSON object:

"Title": "JSON VS BSON",//String Type
"Author": "Abraham Tavarez",
"hasImage": true,// Boolean Type
"readTimeInMinutes": 1,// Number Type
"tags": ["beginners", "javascript"],// Array Type
"references": {
"": "",
"wikipedia": "",
"bsonspec": "",
"mongoDB": ""
},// Embedded documents or objects
"previousArticles": null,// Null Type

JSON has six different data types as we can see in the JSON object example above.

These types are: String, Boolean, Number, Array, Object and Null.
Having only six types makes JSON super easy to understand and work with.

Even though these types allow us to create an impressive amount of expressivity when formatting our data, JSON's capabilities are still very limited.

There are a few other types that are crucial for some applications like databases. This is where BSON comes to the rescue!


BSON is a lightweight binary-encoded serialization format capable of representing any JSON-like document.

You can think of BSON as a superset of JSON. BSON supports all of the JSON types but also contains extensions or extra types that are not part of the JSON spec. BSON has well over a dozen of different types, we're going to see some of those types in an example below but here is a link to the full list.

Let's come back to databases for a second. For databases, working with dates and numbers is crucial. JSON has no Date type and only has a single Number type which is not enough to work with large numbers.

MongoDB database uses BSON format to transfer MongoDB documents between the client and the server.

Documents are encoded to a BSON string before being sent to the server and decoded by the driver before returning it to the client.

Using BSON to represent MongoDB's documents allows MongoDB to share data throughout MongoDB's ecosystem despite the language or driver you are writing your application in while maintaining performance, efficiency, and keeping data easy to traverse.

Let's look at some key details about BSON:

alt text

  • BSON stands for Binary JSON.
  • BSON was created in 2009 by MongoDB.
  • BSON documents are stored as a string of bytes.
  • BSON has a variety of implementations depending on the language/driver you are using. For example JavaScript, Python, Java, C# and many more...
  • BSON is efficient, keeping the space used to a minimum.
  • BSON is easy to traverse, which is useful for analyzing data.
  • BSON is really fast in most programming languages.
  • BSON uses C programing language for creating data types making the encoding and decoding process impressibly fast.

Finally, let's look at the same example but with new BSON types:

"_id": ObjectId(), // Object Type
"Article": "JSON VS BSON",
"Author": "Abraham Tavarez",
"hasImage": true,
"readTimeInMinutes": 1,
"tags": ["beginners", "javascript"],
"references": {
"": "",
"wikipedia": "",
"bsonspec": "",
"mongoDB": ""
"previousArticles": null,
"postedOn": new Date(), // Date Type
"likes": NumberInt("343"),// NumberInt Type 4-byte
"shares": NumberLong("1254"),// NumberLong Type 8-byte
"totalLikes": function(){/your code here.../},// Code Type
"querytags": /beginner/i // Regular Expression Type

BSON Types:

  1. Notice the ObjectId type at the top. ObjectId are the default identifier for documents in MongoDB.
  2. Date Type, notice how we can set the key "postedOn" to an instance of new Date(), pretty nifty.
  3. NumberInt for 4-byte integers and NumberLong for 8-byte integers.
  4. Code can also be store as a value in a BSON Object.
  5. Regular Expressions can also be store on BSON object to work with queries.

Remember, don't forget to read the docs for a full list of BSON types and to learn more about BSON and MongoDB.

Thank you for taking your time to read this short article. I hope it was helpful for you. If you like it please let me know by leaving me a comment, like or share, also if you spot any errors please let me know as well.
Happy hacking πŸ‘¨πŸ»β€πŸ’» 🀘🏻.

Linkedin | Facebook | Github

Danny Nee on Unsplash Thanks for the awesome cover photo!

Top comments (1)

huynhtruong01 profile image

The article is very benefit and it help me understand it, thank you πŸ˜€πŸ˜€