0

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.

Anton Cagle
  • 27
  • 1
  • 4

3 Answers3

0

This will find the matching element frombenefitValues, 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);
user184994
  • 17,791
  • 1
  • 46
  • 52
  • this looks really good, but my actual live json call is returning the array within an empty container array like this [[{"changeDate": "2017-10-13T20:26:13.000+0000", etc, etc }]] How do i get passed the empty container array? – Anton Cagle Dec 21 '17 at 22:06
  • i seemed to get rid of the outer array problem, but i am still getting this error Value: treatment.value, : TypeError: Cannot read property 'value' of undefined – Anton Cagle Dec 21 '17 at 22:51
  • Okay, that means that `find` isn't returning anything, so it's cant find one with that subcategory – user184994 Dec 22 '17 at 00:54
0

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)
CRice
  • 29,968
  • 4
  • 57
  • 70
0

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.