What are the main differences between JSON and BSON formats?


I was reading an article on the Internet , where comments were made about the BSON format ( Json Binário ), but I had some doubts about it:

  • What is BSON ?
  • For what reasons was it created?
  • Is there a big difference in performance between JSON x BSON ?
  • What languages ​​support it?
  • When should I actually use it instead of JSON ?


There is no way to compare the performance, this is very relative , it depends on the layer and in the end it will probably be JSON again (at runtime), there may be a "cache" or something like that, of course in practice how will you have to first read and de-compile, but depending on the software used it may not even feel any difference. After all in the end both json and bson will have to parse them to be used by the application's internal variables.

The format in BSON would be something like:

\x16\x00\x00\x00               // tamanho do documento
\x02                           // 0x02 = tipo String
hello\x00                      // nome do campo
\x06\x00\x00\x00world\x00      // valor do campo
\x00                           // 0x00 = type EOO (Final do objeto)

Equivalent to this {"hello":"world"} .

As I understand it, BSON was created to store or transfer JSON in order to avoid character loss, for example. As the site has 3 goals :

  1. be light

    Keeping overhead to a minimum is important for any data representation format, especially when used over the network.

  2. Transportable

    It was designed to be easily transportable.

  3. Efficiency

    Data encoding to BSON and BSON decoding can be performed very quickly in most languages ​​due to the use of C data types.

BSON is used by MongoDB, but there are a number of libs available at: http://bsonspec.org/implementations.html

Scroll to Top