Converting JSON to BSON Using JavaScript: A Practical Guide
Before diving into the code, ensure you have Node.js installed in your system. Our script utilizes the "fs" module for file handling, the "path" module for file path manipulations, and the "bson" module, a dependency you need to install using npm (Node Package Manager) or yarn.
Here's the command to install the bson module:
Here's the command to install the bson module:
npm install bson
1. Module Importation:
First, we import necessary modules - "fs" for file system operations, "path" for directory traversing, and "bson" for the BSON serialization.
const fs = require('fs'); const BSON = require('bson'); const path = require('path');
2. File Paths Definition:
We define the paths for our source JSON file and the target BSON file, utilizing the 'path' module to ensure our script's compatibility across various OS platforms.
const jsonFilePath = path.join(__dirname, 'binance_btcusdt_1h.json'); const bsonFilePath = path.join(__dirname, 'binance_btcusdt_1h.bson');
3. Reading the JSON File:
Using the 'fs' module, we asynchronously read the content of our JSON file. Proper error handling ensures any issues like file non-existence are caught.
fs.readFile(jsonFilePath, 'utf8', (err, jsonContent) => {
if (err) {
console.error('Error reading file:', err);
return;
}
});
4. Parsing JSON Content:
Inside the callback, we attempt to parse the read content using 'JSON.parse'. We wrap any non-object root elements (like arrays) inside an object since BSON requires the root element to be an object.
let jsonData = JSON.parse(jsonContent);
if (Array.isArray(jsonData)) {
jsonData = { root: jsonData };
}
5. JSON to BSON Conversion:
Upon successful parsing, we convert the JSON data into BSON format using the 'serialize' method from the 'bson' module.
const bsonContent = BSON.serialize(jsonData);
6. Writing to BSON File:
Finally, we asynchronously write the BSON content to a file, providing appropriate feedback upon completion or failure.
fs.writeFile(bsonFilePath, bsonContent, (err) => { if (err) { console.error('Error writing file:', err); } else { console.log('Conversion completed, BSON file saved as', bsonFilePath); } });
Full code:
const fs = require('fs'); const BSON = require('bson'); const path = require('path'); // Path to your source JSON file and target BSON file const jsonFilePath = path.join(__dirname, 'binance_btcusdt_1h.json'); const bsonFilePath = path.join(__dirname, 'binance_btcusdt_1h.bson'); // Reading the JSON file fs.readFile(jsonFilePath, 'utf8', (err, jsonContent) => { if (err) { console.error('Error reading file:', err); return; } try { // Parsing JSON content let jsonData = JSON.parse(jsonContent); // If the root element is an array, wrap it in an object if (Array.isArray(jsonData)) { jsonData = { root: jsonData }; } // Converting JSON data to BSON const bsonContent = BSON.serialize(jsonData); // Writing BSON data to file fs.writeFile(bsonFilePath, bsonContent, (err) => { if (err) { console.error('Error writing file:', err); } else { console.log('Conversion completed, BSON file saved as', bsonFilePath); } }); } catch (parseError) { console.error('Error parsing JSON:', parseError); } });