concept bfj in category data

This is an excerpt from Manning's book Data Wrangling with JavaScript.
In this section, we’ll use a library called bfj or Big-Friendly JSON. This is a nifty library for parsing a streaming JSON file. It’s like what we did with Papa Parse; we’ll encapsulate bfj in a readable JSON stream, pipe it through the convert temperature stream, and then pipe it out to weather-stations-transformed.json using a writable JSON stream, as described in figure 7.11. We’ll reuse the same transformation stream we created earlier, but this time we’ll embed it in our pipeline between the input and output JSON files.
Listing 7.6 Toolkit function to open a JSON file input stream (toolkit/open-json-file-input-stream.js)
const bfj = require('bfj'); const fs = require('fs'); const stream = require('stream'); function openJsonInputStream (inputFilePath ) { #1 const jsonInputStream = new stream.Readable({ objectMode: true }); #2 jsonInputStream._read = () => {}; #3 const fileInputStream = fs.createReadStream(inputFilePath); #4 let curObject = null; #5 let curProperty = null; #6 const emitter = bfj.walk(fileInputStream); #7 emitter.on(bfj.events.object, () => { #8 curObject = {}; #8 }); #8 emitter.on(bfj.events.property, name => { #9 curProperty = name; #9 }); #9 let onValue = value => { #10 curObject[curProperty] = value; #10 curProperty = null; #10 }; #10 emitter.on(bfj.events.string, onValue); #10 emitter.on(bfj.events.number, onValue); #10 emitter.on(bfj.events.literal, onValue); #10 emitter.on(bfj.events.endObject, () => { #11 jsonInputStream.push(curObject); #12 curObject = null; #13 }); emitter.on(bfj.events.endArray, () => { #14 jsonInputStream.push(null); #14 }); #14 emitter.on(bfj.events.error, err => { #15 jsonInputStream.emit("error", err); #15 }); #15 return jsonInputStream; #1 }; #1 module.exports = openJsonInputStream; #16 #1 Toolkit function to open a JSON data stream #2 Instantiates a Readable stream in object mode for reading the input CSV file #3 We must include this; otherwise, we get an error. #4 Creates a stream for reading the input JSON file #5 Tracks the current object that’s being deserialized #6 Tracks the current property that’s being deserialized #7 Walks the JSON data file using bfj #8 This is the callback invoked by bfj when a new object is encountered; we use this to reset the current object. #9 This is the callback when a new property is encountered; we use this to reset the current property. #10 This is the callback when a property value is encountered; we respond to this by storing the property in the current object. Then we build JavaScript objects as the JSON file is incrementally deserialized. #11 This is the callback invoked when the end of an object is encountered. #12 Forwards the current object into the readable JSON data stream #13 At this point, we’ve finished processing a single object, so we reset the current object. #14 This is the callback invoked when the input JSON file has been exhausted, signifying the end of stream. #15 Forwards errors to the writable JSON data stream #16 Exports the toolkit function so that it can be used from other code modulesA point to note in listing 7.6 is how we use bfj’s
walkfunction to walk the structure of the JSON file. The walk terminology is used here because the JSON file is potentially a hierarchical document. It’s potentially structured as a tree, and we must walk (or traverse) said tree to process it, even though in this case we aren’t dealing with a hierarchical document. Instead, we’re assuming that weather-stations.json contains a flat array of data records. As bfj raises its events for the array, and for each object and property, we collect these together and build data records to feed to the JSON data stream through itspushfunction.