How can I parse a large JSON file with repeating values in JavaScript?

我的梦境 提交于 2020-01-05 04:37:17

问题


I am parsing a large JSON file using JSON stream. This works but it returns the file line by line. So when I try and restructure the data I can only get the data that is not repeating.

For example, this is the structure:

{
    "Test": {
        "id": 3454534344334554345434,
        "details": {
            "text": "78679786787"
        },
        "content": {
            "text": 567566767656776
        },
        "content": {
            "text": 567566767656776
        },
        "content": {
            "text": 567566767656776
        }
    }
}

I'm able to get Test.id or Test.details.id but I can only get the First Test.content per line.

I've tried to set in an array but this still gets only the first line of Test.content.

Is there another way to transform a large file other than using JSONStream?

Parsing code:

var getStream = function () {
    let stream = fs.createReadStream(path.join(__dirname, '../test.json'), {
            encoding: 'utf8'
        }),
        parser = JSONStream.parse('*.Test')
    return stream.pipe(parser);
};

getStream()
    .pipe(es.mapSync(function (data) {
        let dataObj = []
        dataObj.push(data)
        processData(dataObj)
    }))


function processData(d) {
    let js = JSON.parse(JSON.stringify(d))
    console.log(js)
    // js.forEach(function (value, index) {
    //     Object.keys(value).forEach(function (v, i) {});

    // })
}


来源:https://stackoverflow.com/questions/58227636/how-can-i-parse-a-large-json-file-with-repeating-values-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!