Though JSON and BSON have near identical names, they are not identical in purpose. BSON is based on JSON but has its own distinct features and advantages. This article will look at these two object notations, uncover their differences, and address any confusion surrounding BSON and JSON.
Table of contents:
- What is BSON?
- How is BSON different from JSON?
- Advantages of BSON
- How to convert JSON to BSON
- Import and export BSON documents
- BSON Document Example
- FAQs
What is BSON?
BSON is a binary encoded Javascript Object Notation (JSON)—a textual object notation widely used to transmit and store data across web based applications. JSON is easier to understand as it is human-readable, but compared to BSON, it supports fewer data types. BSON encodes type and length information, too, making it easier for machines to parse.
Just like JSON, BSON supports various programming languages such as C, C++, C#, Java, JavaScript, PHP, Python, Ruby, and Swift.
What Does BSON Stand For?
BSON stands for Binary Javascript Object Notation. It is a binary-encoded serialization of JSON documents. BSON has been extended to add some optional non-JSON-native data types, like dates and binary data.
BSON can be compared to other binary formats, like Protocol Buffers. The key difference is that BSON is more flexible than Protocol Buffers, offering the advantage of adaptability but potentially sacrificing some space efficiency.
BSON Specifications and BSON Types
BSON specification version 1.1 is published at bsonspec.org. The topmost element in the structure must be of type BSON object and should contain one or more elements, where an element can be a field name (string), a type, or a value. The following are some of the types included in BSON.
To learn more about BSON specifications, visit BSON documentation.
How is BSON Different from JSON?
To know more about the differences between BSON and JSON, visit JSON and BSON.
Advantages of BSON
The following three characteristics make BSON advantageous to use.
Lightweight and Traversable
BSON is lightweight: This makes it possible for a large amount of data to be stored in BSON file format. Additionally, BSON files are easily stored and sent through the network, making them perfect for storing and sending data.
Efficient
BSON was made to efficiently store space and scan: In a BSON document, large elements are prefixed with a length field. Documents in BSON use more space than JSON due to the length prefixes and explicit array indices, but thanks to those prefixes, we are able to scan and query much faster. The prefixes make it easy to compare and calculate directly on data, simplifying application code consumption.
Handles Additional Data Types
Unlike in JSON, you can find data types such as Bindata, Minkey, Maxkey, Binary Data, ObjectID, Regular Expression, JavaScript, Decimal128, and Date for datetime in BSON. These data types are crucial when working with specialty programs.
For example, when working with financial systems data, you can use BSON’s Decimal128 for 128 bits of high precision decimal representation.
Many modern systems use binary-based floating-point arithmetic to represent exact decimal fractions through Float and double data types. These data types provide approximation but not the precise value. Using BSON in such cases gives you high precision.
Visit BSON data types to find the list of datatypes exclusive to BSON but not to JSON.
How to Convert JSON to BSON
You can use various converters between JSON and BSON formats. One such example is OnlineJSONTools. A quick Google search will give you various online converters that easily change the JSON format to BSON format and vice versa.
BSON is the format used both for data storage and network transfer in MongoDB. After storing data in MongoDB, you may want to get your data back as JSON, among many other formats. When you want to export your files as JSON, you can use MongoDB’s database tool, bsondump, to convert BSON documents to JSON. To learn how to export BSON documents as JSONs in MongoDB, read below.
How to Import and Export a BSON File in MongoDB
There are plenty of online BSON text viewers that are capable of opening BSON files. If you have a large set of BSON files, you can import BSON files to MongoDB to see the contents of the file from there.
First, you need to install the MongoDB database tools. MongoDB database tools are a suite of command-line utilities for working with MongoDB. You can learn about installing them from this documentation.
After installing those tools, you can easily import and export BSON files in MongoDB by using a few simple commands. The mongorestore command loads data from either a binary database dump created by mongodump or the standard input into a mongod or mongos instance.
To import a .bson file, run the following command on your system command line.
Here, db_name is the name of the database you want to import. Path denotes the absolute path of your .bson file, while file represents the BSON file’s name.
For a single collection, run the following command on your system command line.
Here, collection_name defines the name of the collection that you want to import.
For restoring the complete folder exported by mongodump.
To export BSON documents, use the database tool bsondump.
Run the following command from the system command line to export the collection as BSON.
You can use the --outFile option by running the following command from the system command line to export the collection as JSON.
You can check out the official documentation for bsondump to learn how to install bsondump to convert BSON files to human-readable formats, including JSON.
In case of a problem with the programming language, be sure to update the MongoDB driver to the version that supports the language.
BSON Example: How BSON is Stored in MongoDB Database
Let’s take the following JSON document example.
When storing the JSON document, it will be converted to the following.