如何使用Node.js解析具有JSON对象数组的文件?

我有一个项目,我想从具有重复属性的json对象数组的文件中获取JSON数据:

一些文件:(每个对象在一行中,为简单起见,我对其进行了格式化)

{
   "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"
}

我想获取某些特定项目的统计信息,例如Country和AppVersion。 如何使用NodeJ处理它以产生以下输出:

{
   "stats":{
      "Country":{
         "USA":"2",
         "SPAIN":"1"
      },
      "AppVersion":{
         "1.3.4":"2",
         "1.3.5":"1"
      }
   }
}
评论
  • bquia
    bquia 回复

    You can use array.reduce to accumulate the count values:

    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 });