I've been trying to read a file using JSONStream but I don't have much experience in this and it's been hard to find information (tutorials, documentation) about it.
I found somewhere on here this piece of code:
var fs = require('fs'),
JSONStream = require('JSONStream');
var stream = fs.createReadStream('tst.json', {encoding: 'utf8'}),
parser = JSONStream.parse();
stream.pipe(parser);
console.log(parser);
parser.on('root', function (obj) {
console.log(obj); // whatever you will do with each JSON object
});
And I was trying to use it with a json test file like this:
{
"colors": [{
"color": "black",
"category": "hue",
"type": "primary",
"code": {
"rgba": [255, 255, 255, 1],
"hex": "#000"
}
},
{
"color": "white",
"category": "value",
"code": {
"rgba": [0, 0, 0, 1],
"hex": "#FFF"
}
},
{
"color": "red",
"category": "hue",
"type": "primary",
"code": {
"rgba": [255, 0, 0, 1],
"hex": "#FF0"
}
},
{
"color": "blue",
"category": "hue",
"type": "primary",
"code": {
"rgba": [0, 0, 255, 1],
"hex": "#00F"
}
},
{
"color": "yellow",
"category": "hue",
"type": "primary",
"code": {
"rgba": [255, 255, 0, 1],
"hex": "#FF0"
}
},
{
"color": "green",
"category": "hue",
"type": "secondary",
"code": {
"rgba": [0, 255, 0, 1],
"hex": "#0F0"
}
}
]
}
And I thought it would return all the objects but nothing happened, it doesn't even goes in the "parser.on('root', function (obj)". What can I do to make this work?