Binary JSON (BSON)

To understand BSON we have to go back to JSON to understand why it was designed and that is to provide a light weight medium for transferring the data over the wire that can be serialized and deserialized very fast. BSON took this thing one step ahead.

Binary JSON or BSON is the binary version of JSON i.e javascript object notation.In simple terms we can also say that it is an extension over the conventional JSON.It supports all the features that are available in JSON that means anything that can be represented or processed in JSON can be done in BSON also,in addition to that BSON also provides some extra advantage over JSON.
BSON Specs can be seen here.

Below are the advantages that BSON gives you – .

Data Types : Additional Data types like Date have been introduced in BSON.
e.g : Here we can see how dates are represented in a JSON and BSON document.

  • JSON Doc –
    > today={"createdDate":"Mon Mar 31 2014 11:21:51 GMT+0000 (GMT)"}
    { "createdDate" : "Mon Mar 31 2014 11:21:51 GMT+0000 (GMT)" }

    Here, date will be stored as a string and whenever we want to search for a particular date first it needs to parse it to the date value and then only it can compare the values. So, parsing is always an extra overhead in case when we are dealing with dates in JSON.

  • BSON Doc – BSON provides you a Date data type that stores the Date as 64 bit integers of the Date class and retrieves the value as Javascript Date.
    > todayBSONDate={"createdDate":new Date("Mon Mar 31 2014 11:21:51 GMT+0000 (GMT)")}
    { "createdDate" : ISODate("2014-03-31T11:21:51Z") }

    Here, as we can see Date has been stored as actual Javascript Date type , so an extra overhead of parsing the date string is not here.

Prefix Length : BSON stores more metadata in comparison to JSON. One of such metadata is the length prefixes that it uses for fast traversal.These prefixes are used to skip a set of data that you are not interested in to traverse.
e.g : I have some sample data like this containing an array of strings.

["This is the first very very long string , only God and Rajnikanth knows when it will end,",
"This is the second very very long string and this time only Rajnikanth knows when it will end ............",
"Here is what I am looking for"]

Basically I am interested in finding the third string i.e “Here is what I am looking for” in the array. Now when JSON will start parsing it will parse both the strings that are coming before the string that we are looking for. So , here is an extra overhead of parsing two strings.
On the contrary when BSON will store this document it will also add prefix length metadata information with it.
This is not actual markup, only for understanding the problem statement I am adding the markup like this.

[89 "This is the first very very long string , only God and Rajnikanth knows when it will end,",
106 "This is the second very very long string and this time only Rajnikanth knows when it will end ............",
29 "Here is what I am looking for"]

Now when BSON will parse this document it can skip the first two string using prefix length that it has stored as metadata ,as we are interested in the third string of the array.

These are the only two new things that BSON provides you over JSON , again chosing BSON Vs JSON purely depends on the requirement of your problem statement.

Please share your comments and feedback about the post or let me know if I have represented any of the information or facts wrongly.


Saurabh Jain

A Developer working on Enterprise applications ,Distributed Systems, Hadoop and BigData.This blog is about my experience working mostly on Java technologies ,NoSQL ,git , maven and Hadoop ecosystem.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

2 thoughts on “Binary JSON (BSON)

Add Comment Register

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>