我有一个项目,我想从包含具有重复属性的 json 对象数组的文件中获取 JSON 数据:
some-file:(每个对象都在 1 行中,为了简单起见,我对其进行了格式化)
{
"A":"ALL",
"B":"3349256522",
"Location":{
"Country":"USA"
},
"EffectiveDate":"2020-03-04T14:15:52.063Z",
"Demographic":{
"Q":"done",
"G":"ok",
"AppVersion":"1.3.4",
},
"ApplicationId":"92398723892937",
"Id":"23232993939333",
"CreationDate":"2020-03-04T14:15:52.063Z"
}
{
"A":"NONE",
"B":"8469256522",
"Location":{
"Country":"SPAIN"
},
"EffectiveDate":"2020-03-04T14:15:52.063Z",
"Demographic":{
"Q":"done",
"G":"ok",
"AppVersion":"1.3.5",
},
"ApplicationId":"92398723892937",
"Id":"23232993939333",
"CreationDate":"2020-03-09T14:15:52.063Z"
}
{
"A":"ALL",
"B":"8469256522",
"Location":{
"Country":"USA"
},
"EffectiveDate":"2020-03-04T14:15:52.063Z",
"Demographic":{
"Q":"done",
"G":"ok",
"AppVersion":"1.3.4",
},
"ApplicationId":"92398723892937",
"Id":"23232993939333",
"CreationDate":"2020-03-11T14:15:52.063Z"
}
我想获取特定项目、国家/地区和应用程序版本的一些统计信息。 我如何使用 NodeJs 处理它以产生此输出:
{
"stats":{
"Country":{
"USA":"2",
"SPAIN":"1"
},
"AppVersion":{
"1.3.4":"2",
"1.3.5":"1"
}
}
}
此外,输入文件“some-file”不是有效的 JSON 文件,有什么建议如何将其转换为有效(在代码中)?
The original file is (1 line each object):
{ "A":"ALL", "B":"3349256522", "Location":{ "Country":"USA" }, "EffectiveDate":"2020-03-04T14:15:52.063Z", "Demographic":{ "Q":"done", "G":"ok", "AppVersion":"1.3.4" }, "ApplicationId":"92398723892937", "Id":"23232993939333", "CreationDate":"2020-03-04T14:15:52.063Z"}
{ "A":"ALL", "B":"3349256522", "Location":{ "Country":"SPAIN" }, "EffectiveDate":"2020-03-04T14:15:52.063Z", "Demographic":{ "Q":"done", "G":"ok", "AppVersion":"1.3.5" }, "ApplicationId":"92398723892937", "Id":"23232993939333", "CreationDate":"2020-03-04T14:15:52.063Z"}
{ "A":"ALL", "B":"3349256522", "Location":{ "Country":"ITALY" }, "EffectiveDate":"2020-03-04T14:15:52.063Z", "Demographic":{ "Q":"done", "G":"ok", "AppVersion":"1.3.4" }, "ApplicationId":"92398723892937", "Id":"23232993939333", "CreationDate":"2020-03-04T14:15:52.063Z"}
最佳答案
您可以使用array.reduce累计计数值:
let input = [{
"A":"ALL",
"B":"3349256522",
"Location":{
"Country":"USA"
},
"EffectiveDate":"2020-03-04T14:15:52.063Z",
"Demographic":{
"Q":"done",
"G":"ok",
"AppVersion":"1.3.4",
},
"ApplicationId":"92398723892937",
"Id":"23232993939333",
"CreationDate":"2020-03-04T14:15:52.063Z"
},
{
"A":"NONE",
"B":"8469256522",
"Location":{
"Country":"SPAIN"
},
"EffectiveDate":"2020-03-04T14:15:52.063Z",
"Demographic":{
"Q":"done",
"G":"ok",
"AppVersion":"1.3.5",
},
"ApplicationId":"92398723892937",
"Id":"23232993939333",
"CreationDate":"2020-03-09T14:15:52.063Z"
},
{
"A":"ALL",
"B":"8469256522",
"Location":{
"Country":"USA"
},
"EffectiveDate":"2020-03-04T14:15:52.063Z",
"Demographic":{
"Q":"done",
"G":"ok",
"AppVersion":"1.3.4",
},
"ApplicationId":"92398723892937",
"Id":"23232993939333",
"CreationDate":"2020-03-11T14:15:52.063Z"
}];
let result = input.reduce((acc,current) => {
let c = current.Location.Country;
let v = current.Demographic.AppVersion;
if(acc.Country[c]){
acc.Country[c]++;
} else {
acc.Country[c] = 1;
}
if(acc.AppVersion[v]){
acc.AppVersion[v]++;
} else {
acc.AppVersion[v] = 1;
}
return acc;
}, {Country: {}, AppVersion: {}});
console.log({ stats: result });
关于javascript - 如何使用 Node.js 解析包含 JSON 对象数组的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61110525/