I have a 300k line JSON file that I'm trying to parse in Node using JSONStream. From the docs, when doing the following, I'm expecting the first 10 rows of the file to be output to the console, however I get the entire document as a string, with \n
characters between each row:
var fs = require('fs');
var JSONStream = require('JSONStream');
var i = 0;
var stream = fs.createReadStream('test.json', {encoding: 'utf8'})
stream.pipe(JSONStream.parse('*'))
stream.on('data', function(data) {
if(i < 10){
console.log(i, data)
}
i++;
});
Shouldn't JSONStream.parse("*")
be...parsing the JSON? What am I doing wrong here?
You have to register the event handler on the jsonstream obj:
var jsonStream = JSONStream.parse('*')
stream.pipe(jsonStream)
jsonStream.on('data', function(data) {
// process data
})