concept BSON in category data

appears as: BSON
Data Wrangling with JavaScript

This is an excerpt from Manning's book Data Wrangling with JavaScript.

4.4.3 Replacing JSON with BSON

BSON, or binary JSON, is a binary encoded serialization of JSON. Although you can’t open a BSON file in a text editor, it is (like JSON) a self-describing format. You don’t need documentation to understand or remember how to decode the data file.

BSON is a standard and mature data format. It’s the format that underlies MongoDB. It’s almost a drop-in replacement for JSON, and it’s easy to convert between JSON and BSON.

BSON will allow you to store your JSON in a more compact way. This might be useful if you are trying to save disk space or network bandwidth. BSON won’t gain you anything in performance, though, because it’s slightly slower than JSON serialization. To use BSON, you therefore must make a tradeoff between size and performance.

Listing 4.5 shows how to convert earthquakes.json to a BSON file. We instance the BSON object and use its serialize function to convert our JavaScript data to the binary BSON format. The result is a Node.js Buffer object that we write to our new data file earthquakes.bson. You can run the code for the following listing, and it will convert the example file earthquakes.json to the BSON file earthquakes.bson.

Listing 4.5 Converting JSON data to BSON (listing-4.5.js)
const fs = require('fs');
const moment = require('moment');
const BSON = require('bson');

const records = JSON.parse(  #1  
    fs.readFileSync("./data/earthquakes.json", "utf8") 
); 

for (let i = 0; i < records.length; ++i) {   #2  
 const record = records[i];   #2  
 record.Time = moment(record.Time).toDate();   #2  
}   #2  

const bson = new BSON();   #3  
const serializedData = bson.serialize(records);   #4  

fs.writeFileSync("./output/earthquakes.bson", serializedData);   #5  

#1   Loads earthquakes.json that we’re going to convert to BSON
#2   For each record, parse the Time value from a string to a Date object. Unlike JSON, BSON can store actual Date objects.
#3   Instances a BSON object
#4   Serializes our data to a Node.js Buffer object
#5   Writes the buffer to the binary file earthquakes.bson
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest