1

I cant wrap my head on how to fetch value from a key Value json pair.

My JSON looks like this:

data= [
  {
    "flowId": 7079,
    "flowName": "jackson-demo",
    "version": 1,
    "CreatedDate": "2020-04-02",
    "UpdateDate": "",
    "LastRunDate": "2020-04-02",
    "active": false,

"properties": [
  {
    "id": 7080,
    "key": "country",
    "value": "in",
    "category": "General"
  },
  {
    "id": 7081,
    "key": "source",
    "value": "hive",
    "category": "General"
  }
  ]

  },
  {

"flowId": 7079,
"flowName": "jackson-demo",
"version": 1,
"CreatedDate": "2020-04-02",
"UpdateDate": "",
"LastRunDate": "2020-04-02",
"active": false,

"properties": [
  {
    "id": 7080,
    "key": "country",
    "value": "au",
    "category": "General"
  },
  {
    "id": 7081,
    "key": "source",
    "value": "aws",
    "category": "General"
  }
  ]

} ]

How do I get value of Country or Source. I am able to iterate and get value of all the Properties array using below code but how do I get value of only country from each Array Objects.

 data.forEach(function(obj) 
{ 
  console.log(obj.properties); 
});
0

4 Answers 4

2

Use a basic forEach loop to iterate over items.

EDIT:

To print all values

data.forEach((arrayItem) => {
  arrayItem.properties.forEach(item => {
    console.log(item);
  });
});

var data = [{
    flowId: 7079,
    flowName: "jackson-demo",
    version: 1,
    CreatedDate: "2020-04-02",
    UpdateDate: "",
    LastRunDate: "2020-04-02",
    active: false,

    properties: [{
            id: 7080,
            key: "country",
            value: "in",
            category: "General"
        },
        {
            id: 7081,
            key: "source",
            value: "hive",
            category: "General"
        }
    ]
},
{
    flowId: 7079,
    flowName: "jackson-demo",
    version: 1,
    CreatedDate: "2020-04-02",
    UpdateDate: "",
    LastRunDate: "2020-04-02",
    active: false,

    properties: [{
            id: 7080,
            key: "country",
            value: "au",
            category: "General"
        },
        {
            id: 7081,
            key: "source",
            value: "aws",
            category: "General"
        }
    ]
}
];
data.forEach(function(arrayItem, i) {
console.log(arrayItem.properties[i]);
});

data.forEach((arrayItem, i) => {
  arrayItem.properties.forEach(item => {
console.log(item);
  });
});

Sign up to request clarification or add additional context in comments.

1 Comment

Your code returns Keys only for first Object.I want to iterate on all Objects and fetch Keys
2

Basically, data is an array so first you have to access the first element before accessing the properties object. And then using forEach you can iterate through it and access the values.

data = [{
    "flowId": 7079,
    "flowName": "jackson-demo",
    "version": 1,
    "CreatedDate": "2020-04-02",
    "UpdateDate": "",
    "LastRunDate": "2020-04-02",
    "active": false,

    "properties": [{
        "id": 7080,
        "key": "country",
        "value": "in",
        "category": "General"
      },
      {
        "id": 7081,
        "key": "source",
        "value": "hive",
        "category": "General"
      }
    ]

  },
  {

    "flowId": 7079,
    "flowName": "jackson-demo",
    "version": 1,
    "CreatedDate": "2020-04-02",
    "UpdateDate": "",
    "LastRunDate": "2020-04-02",
    "active": false,

    "properties": [{
        "id": 7080,
        "key": "country",
        "value": "au",
        "category": "General"
      },
      {
        "id": 7081,
        "key": "source",
        "value": "aws",
        "category": "General"
      }
    ]
  }
];
var array = Object.keys(data);
array.forEach(i => {
  properties = data[i].properties; //gets the properties array object from data
  properties.forEach(obj => { //gets each object within the properties array
    console.log(obj.key);
    console.log(obj.value);
  })
})

3 Comments

Your solution only returns value for first object.What if I have to iterate over all the Object and get the Key Value Pair
Do you want it to iterate over all key-value pairs within the data array?
@SaadAhmed I have updated the code as per requirement
1

For every property object you iterate, create an empty object={ }

obj[propertyObj["key"]]=propertyObj["value"]

after this return the object.

Run the following code snippet to verify

let data= [{"flowId": 7079,"flowName": "jackson-demo","version": 1,"CreatedDate": "2020-04-02","UpdateDate": "","LastRunDate": "2020-04-02","active": false,
  "properties": [{"id": 7080,"key": "country","value": "in","category": "General"},{"id": 7081,"key": "source","value": "hive","category": "General"}]},{"flowId": 7079,"flowName": "jackson-demo", "version": 1, "CreatedDate": "2020-04-02","UpdateDate": "","LastRunDate": "2020-04-02","active": false,
  "properties": [{ "id": 7080,"key":"country","value": "au","category":"General"},{"id": 7081,"key": "source","value":"aws","category": "General"}]
}]
console.log(data.map(item =>{
   let obj={};
   for (const propertyObj  of item.properties)
      obj[propertyObj["key"]]=propertyObj["value"]
   return obj;
}));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

0

Your JSON structure is as below: data = array node with multiple object nodes, properties = array node with multiple object nodes within each "data" object node, key/value = simple string json node for each "property" object node.

So, first we need to iterate through "data" array node and for each data object node, fetch the "properties" array node and retrieve "key" and "value" for each property object node.

var data= [
      {
        "flowId": 7079,
        "flowName": "jackson-demo",
        "version": 1,
        "CreatedDate": "2020-04-02",
        "UpdateDate": "",
        "LastRunDate": "2020-04-02",
        "active": false,
    "properties": [
      {
        "id": 7080,
        "key": "country",
        "value": "in",
        "category": "General"
      },
      {
        "id": 7081,
        "key": "source",
        "value": "hive",
        "category": "General"
      }
      ]
      },
      {
    "flowId": 7079,
    "flowName": "jackson-demo",
    "version": 1,
    "CreatedDate": "2020-04-02",
    "UpdateDate": "",
    "LastRunDate": "2020-04-02",
    "active": false,
    "properties": [
      {
        "id": 7080,
        "key": "country",
        "value": "au",
        "category": "General"
      },
      {
        "id": 7081,
        "key": "source",
        "value": "aws",
        "category": "General"
      }
      ]
     }
    ];

    data.forEach(dataItem=>{
    dataItem.properties.forEach(property=>{
      document.write("key: "+property.key+" ,");
      document.write("value: "+property.value+"<br>");
      });
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.