Binary JSON (BSON)

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

In this post we shall understand the circumstances behind the need for Binary JSON (BSON), it's applications, merits and demerits and also most importantly it's relationship with JSON.

But inorder to understand BSON Let's first get a better understanding of JSON .

What is JSON?

JSON also referred to as Javascript Object Notation is a standard file format consisting of human readable text which is mainly used for asynchronous client and server communication.

Need for JSON
The need for JSON arose when it became essential to communicate between servers in file format and the data could only be text.JSON solved this problem as javascript objects could be converted to JSON and then sent to the server.And on the server side the javascript objects could be retrieved back from the JSON format.

As JSON's popularity grew it soon began to be used in software applications one of them being database storage.

Problems with JSON format of Data Storage
There are certain problems with JSON that make it inefficent for usage in a database.
1.Since JSON is text based format it takes longer to parse it.
2.JSONs readable format is quite space consuming hence not the best format based on space considerations.
3.JSON does not provide a large number of advanced data types.
The above problems paved the way for BSON-which is an extended version of JSON.

Understanding BSON

BSON does not in any way replace JSON but is in fact a Interchange Format particularly used in storage and as a network transfer format used in the MongoDB database.

(Note:-BSON is not specific to MongoDB it can be used with several other languages as well.)

BSON can be thought of as a super set of JSON, consisting of all properties that JSON has with some additional features like:
1.LightWeight:
BSON is designed to have minimum space overhead which is preferred for any data representation format over a network.
2.Easily Traversable:This is one of its most important feature and the reason why MongoDB uses BSON,is that query can be accessed in lesser time than before.
3.Less Time Required for Encoding Or Decoding:The data types used by BSON are easy to encode and decode which ensures faster operation capabilities.

Before we realise the additional data types that BSON provides us with lets get a better understanding of some of its basic data types

Understanding BSON Array and Objects

A type-safe representation of the BSON array type:

public class BsonArray
extends BsonValue
implements List<BsonValue>, Cloneable

Constructing empty array in BSON:

BsonArray()

wheras an empty array in JSON would be constructed like:

var array_name = [];

Constructing an instance with given list of values in BSON:

BsonArray(List<? extends BsonValue> values)

Constructing an instance with given list of values in JSON:

Array.from(arrayLike [, mapFn [, thisArg]])

where arrayLike: An array-like or iterable object to convert to an array

BSON types:

The below mentioned values can be used with the "$type" operator to query documents by their BSON type.

1.Double:This type is used to store floating point values

{"$numberDouble": "<decimal string>" }

2.String: This is the most commonly used datatype to store the data. MongoDB stores string only if it is UTF-8 valid.
3.Object:This datatype is used for embedded documents.
JSON objects are
4.Array:This type is used to store arrays or list or multiple values into one key
BSON array can be represented as

[“hello”,{“$numberInt”:”10”}]

Whereas JSON array would be represented as

["hello","10"]

5.Binary data:This datatype is used to store binary data.The below code represents how binary data is stored as BSON format.

{ "$binary":
   {
      "base64": "<payload>",
      "subType": "<t>"
   }
}

There is no such available datatype in JSON.
6.Undefined
7.ObjectId:This datatype is used to store the document’s ID

{ "$oid": "<ObjectId bytes>" }

8.Boolean:This type is used to store a boolean (true/ false) value
9.Date:This datatype is used to store the current date or time in UNIX time format. You can specify your own date time by creating object of Date and passing day, month, year into it.
BSON

{"$date": {"$numberLong": "<ISO-8601 Date/Time Format>"}}

JSON

{"date" : ISODate("2014-02-10T10:50:42.389Z")}

10.Null:This type is used to store a Null value
11.Regular Expression

{“$regularExpression”:{“pattern”:”^H”,”options”:”i”}}

12.Timestamp:This can be handy for recording when a document has been modified or added.

{“$timestamp”:{“t”:1565545664,”i”:1}}

13.Min key:This type is used to compare a value against the lowest BSON elements.


{ "$maxKey": 1 }

14.Max key:This type is used to compare a value against the highest BSON elements


{ "$maxKey": 1 }

15.Decimal128

{ "$numberDecimal": "<number>" }

16.64-bit integer
A 64 bit integer will be represented as:

{ "$numberLong": "<number>" }

17.JavaScript
18.JavaScript (with scope)
19.Symbol
20.Int32

{ "$numberInt": "<number>" }

BSON VS JSON

Binary JSON differs from JSON depending on may different categories like:
1.Type Of File
JSON is used for storing files of standard file format consisting of human as well as machine readable text whereas BSON stores binary file format only which can be read by the machine only.


2.Structure
BSON consists a list of ordered elements with fieldname,type of field-data,and the value of the field.

3.Encoding and Decoding Techniques
Since BSON is a serialization format it invloves decoding BSON and re-encoding JSON.

BSON is designed in a way that it has a comparatively faster encoding and decoding technique. For example, all those integers stored as 32-bit integers so that they are not parsed with them to and from the text. Therefore it makes use of more space as compared to JSON for smaller integers but BSON is anyway much faster to parse.



4.Traversal
Unlike JSON, BSON doesn't skim through the entire file but indexes the relevant content and skips all content that does not have to be used.

5.Data Types
The main advantage of BSON over JSON would be the availability of additional data types such as Date and BinData data types along with the basic data types like numbers, strings, and other Boolean values.
The different data types provided by BSON include:

  1. double (64-bit IEEE 754 floating point number)
  2. date (integer number of milliseconds since the Unix epoch)
  3. byte array (binary data)
  4. BSON object and BSON array
  5. MD5 binary data

Disadvantages

BSON has a slight disadvantage when it comes to memory efficiency as it requires some overhead to create additional fields like field name,field value etc.

Applications

1.Researches have shown that BSON produces output which is much smaller than JSON (upto 25% ) therefore it would be quite practical to include BSON in our Web APIs.As it is easier to encode and decode and there is no need to switch between simpler data types and their string representation while serializing and deserializing data.

2.MongoDB uses BSON to store documents and make remote procedure calls in MongoDB.

With this article at OpenGenus, you must have the complete idea of Binary JSON. Enjoy.

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.