difference between json and bson

JSON and BSON in MongoDB

In this post, we will learn the difference between JSON and BSON in MongoDB. These two are document formats and they are widely used by developers in different use cases. In MongoDB, the data is stored in BSON and viewed in JSON.

Instead of going directly to see the difference between JSON and BSON let’s understand both of them first.

What is JSON?

JSON stands for JavaScript Object Notation. What Wikipedia definition says about JSON is:

JSON (JavaScript Object Notation) is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute-value pairs and arrays.

This basically means JSON is a file format that can be used to store data in a file system and can be used as a message format while exchanging data between networks. And the most important is that all the data are in a readable format.

JSON format

A document can be in a JSON format if and only if it satisfies the following:

  • Start and end with curly braces { }
  • Each Key and Value should be separated with a colon :
  • Each key : value pair should be separated with a coma ,
  • All keys should be inside a double quote “myKey”

A valid JSON document looks like this:

{
  "id": 101,
  "name": "Coder Sathi",
  "department": "Engineering",
  "salary": 50000,
  "address": {
    "city": "Kathmandu",
    "area": "Pashupatinath"
  }
}

The above document satisfies all the items mentioned.

The invalid JSON document looks like the following:

{
  id: 101,
  "name": "Coder Sathi",
  "department": "Engineering",
  "salary": 50000,
  "address": {
    "city": "Kathmandu",
    "area": "Pashupatinath"
  }
}

This document is exactly the same as the earlier document except the key id is not surrounded by a double quote.

These two document looks identical. There are many tools and websites to validate JSON documents but I prefer to use the website JSON Editor Online.

If you try to validate these documents by visiting this website you can notice the error message for the second JSON. You can also play with the document and see when we can get an error while validating.

Pros of JSON

  • JSON document is a user friendly, readable and familiar for many developers.

Cons of JSON

  • JSON is text-based document. Hence it is very slow to parse.
  • It consumes more space because of readability
  • It supports limited data types like String, Boolean, Number, and Array.

What is BSON?

BSON stands for Binary JSON. Which simply means the binary form of the JSON. As per Wikipedia, it originated in 2009 at MongoDB. MongoDB converts JSON documents to BSON and stores them in the database.

As a developer, we don’t have to know it much since the driver we use to connect to the MongoDB database handles everything like Encoding and Decoding between JSON and BSON.

Since the BSON document is in a binary format we can’t read it clearly. So only the driver and MongoDB database can understand it.

Difference between JSON and BSON

The main difference between JSON and BSON is the following:

JSONBSON
EncodingIt is encoded to UTF-8 StringBSON is encoded to Binary
Data TypesData type support: String, Boolean, Number, ArrayIt also supports String, Boolean, Number, Array, and non-native data types like Date and Binary.
ReadabilityHuman and MachineMachine only


Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments