简体   繁体   中英

Looping Through Nested JSON with Nested Child Searches

I have a JSON structure that looks like this:

"benefitValues" : [ {
          "changeDate" : "2017-10-13T20:26:13.000+0000",
          "changeUserName" : "aaaa",
          "numericValue" : 20,
          "value" : "20",
          "amountType" : {
            "allowCustomDataFlg" : false,
            "dataType" : "Percent",
            "defaultTypeFlg" : true,
            "defaultValue" : "Unlimited",
            "description" : null,
            "maxValue" : null,
            "minValue" : null,
            "name" : "LIST",
            "benefit" : {
              "category" : "Facility Services",
              "name" : "Single Limit",
              "networkStatus" : "IN_NETWORK",
              "planType" : "MedicalPlan",
              "sortOrder" : 20,
              "subcategory" : "Acupuncture Treatment",
              "subcategorySortOrder" : 6
            }
          }
        }]

Based on the string "Acupuncture Treatment", I need to extract the the value and the datatype. The dataset is very large, with hundreds of subcategories. I cannot find a good way to search through this data. I tried json-path and advanced-json-path, but if I do a search on a child element, there is no way for me to return the parents. I want my output to look like this:

{
          "Subcategory" : "Acupuncture Treatment",
          "Value" : "20",
          "Type" : "Percent"
}

I was hoping there was an easy way to do this with an existing library, or at least with a simple loop.

This will find the matching element from benefitValues , and transform the element into the format you're expecting:

 var benefitValues = [{ "changeDate": "2017-10-13T20:26:13.000+0000", "changeUserName": "aaaa", "numericValue": 20, "value": "20", "amountType": { "allowCustomDataFlg": false, "dataType": "Percent", "defaultTypeFlg": true, "defaultValue": "Unlimited", "description": null, "maxValue": null, "minValue": null, "name": "LIST", "benefit": { "category": "Facility Services", "name": "Single Limit", "networkStatus": "IN_NETWORK", "planType": "MedicalPlan", "sortOrder": 20, "subcategory": "Acupuncture Treatment", "subcategorySortOrder": 6 } } }]; // Find the element let treatment = benefitValues.find((item) => item.amountType.benefit.subcategory === 'Acupuncture Treatment'); let result = { Value: treatment.value, Subcategory: treatment.amountType.benefit.subcategory, Type: treatment.amountType.dataType } console.log(result); 

You can search through your data set and pull out only the items that match your string by using .filter . That would give you the entire object, so then you can use .map to transform it to the structure you want.

Or if you're only interested in the first result, you can use .find instead.

 const json = {"benefitValues" : [{ "changeDate" : "2017-10-13T20:26:13.000+0000", "changeUserName" : "aaaa", "numericValue" : 20, "value" : "20", "amountType" : { "allowCustomDataFlg" : false, "dataType" : "Percent", "defaultTypeFlg" : true, "defaultValue" : "Unlimited", "description" : null, "maxValue" : null, "minValue" : null, "name" : "LIST", "benefit" : { "category" : "Facility Services", "name" : "Single Limit", "networkStatus" : "IN_NETWORK", "planType" : "MedicalPlan", "sortOrder" : 20, "subcategory" : "Acupuncture Treatment", "subcategorySortOrder" : 6 } } }]}; // With filter/map const result = json.benefitValues .filter(val => val.amountType.benefit.subcategory === "Acupuncture Treatment") .map(val => ({Subcategory: val.amountType.benefit.subcategory, Value: val.value, Type: val.amountType.dataType})); console.log(result) // With find / manual transform: const singleFullResult = json.benefitValues .find(val => val.amountType.benefit.subcategory === "Acupuncture Treatment") const singleResult = { Subcategory: singleFullResult.amountType.benefit.subcategory, Value: singleFullResult.value, Type: singleFullResult.amountType.dataType } console.log(singleResult) 

You can use Array.prototype.filter() combined with Array.prototype.map() and create an array of object with the structure you need. Here's an example:

 let myArray = [{ "changeDate": "2017-10-13T20:26:13.000+0000", "changeUserName": "aaaa", "numericValue": 20, "value": "20", "amountType": { "allowCustomDataFlg": false, "dataType": "Percent", "defaultTypeFlg": true, "defaultValue": "Unlimited", "description": null, "maxValue": null, "minValue": null, "name": "LIST", "benefit": { "category": "Facility Services", "name": "Single Limit", "networkStatus": "IN_NETWORK", "planType": "MedicalPlan", "sortOrder": 20, "subcategory": "Acupuncture Treatment", "subcategorySortOrder": 6 } } }]; let ret = myArray .filter(arr => arr.amountType.benefit.subcategory === 'Acupuncture Treatment') .map(arr => { return { Subcategory: arr.amountType.benefit.subcategory, Value: arr.value, Type: arr.amountType.dataType }; }); console.log(ret); 

First the filter function will filter your array and return only the items related to 'Acupuncture Treatment', then the map function, that receives as a parameter a function that will be executed for each item inside the array and it will return a new structure, will return only the fields you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM