Nodejs: JSONStream parse method regex



我有这样的有效负载

{
    "rows": [{
        "id": "1234",
        "data": {
            "updatedby": "uid1",
            "resource": {
                "resourceid": "abcd"
            }
        }
    }, {
        "id": "1235",
        "data": {
            "updatedby": "uid2",
            "resource": {
                "resourceid": "pqrs"
            }
        }
    }, {
        "id": "1236",
        "data": {
            "updatedby": "uid3",
            "resource": {
                "resourceid": "bert"
            }
        }
    }]
}

我只需要从 json 有效负载中提取资源标签的内容。你能帮我制定正则表达式吗?以下是我尝试的,它没有调用解析器.data方法。

var parser = JSONStream.parse(['rows', true, /^resource/]);
parser.on('data', function(data) {
    console.log('received the payload -do something');
});

你不需要正则表达式:

var JSONStream = require('JSONStream');
var fs = require('fs');
fs.createReadStream('data.json')
.pipe(JSONStream.parse('rows.*.data.resource'))
.on('data', console.log.bind(console))

其中输出:

{ resourceid: 'abcd' }
{ resourceid: 'pqrs' }
{ resourceid: 'bert' }

最新更新