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?
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?
Share asked Mar 14, 2018 at 22:41 ifhyifhy 4281 gold badge5 silver badges11 bronze badges 1- 1 JSONstream hasn't been maintained since 2018. You may want to evaluate stream-json. – Dan Dascalescu Commented May 24, 2023 at 22:37
1 Answer
Reset to default 4root
event has been removed from JSONStream
. Use data
event instead. https://github./dominictarr/JSONStream/mit/97d973ac59d0e58748cec98ea87aae36e057d368
Also should specify the JSON path as a parameter for JSONStream.parse()
. For your JSON it could be JSONStream.parse('colors.*')
So putting everything together, it should be
var fs = require('fs'),
JSONStream = require('JSONStream');
var stream = fs.createReadStream('tst.json', {encoding: 'utf8'}),
parser = JSONStream.parse('colors.*');
stream.pipe(parser);
parser.on('data', function (obj) {
console.log(obj); // whatever you will do with each JSON object
});