最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using JSONStream to read large JSON files - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 4

root 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
});
发布评论

评论列表(0)

  1. 暂无评论