简体   繁体   中英

Iterating over a json array whose key value is dynamic

I have a json structure as:

{
    "TestCaseList": [
        {
            "TC_1": {
                "name":"verifyloginpagedetails",
                 "value":"2"
            },

            "TC_2": {
                "name":"verify registration page details",
                "value":"3"
            }
        }
    ],
    "Summary": {
        "v":[ 
            {
                "name":"over the ear headphones - white/purple",
                "value":1
            }
        ]
    }
}

How to extract the values name, value of TC_1 , TC_2 where TC_1 is dynamic ie key of TestCaseList?

You can use the Object.keys method to get an array of the keys of an object.

With a single object in the array at "TestCaseList" in your JSON object, this will work:

// jsonObj is your JSON
testCaseKeys = Object.keys(jsonObj.TestCaseList[0]);

If, however, the array at "TestCaseList" contains more than one one element, you can use this to get each set of keys in an individual array:

testCaseKeySets = jsonObj.TestCaseList.map(obj => Object.keys(obj));

I'm sure a more elegant solution exists, but this will do the trick.

var myObj = {
  "TestCaseList":
    [{
        "TC_1":
        {"name":"verifyloginpagedetails",
         "value":"2"},

        "TC_2":
        {"name":"verify registration page details",
         "value":"3"}
    }],
  "Summary":{
    "v":[{"name":"over the ear headphones - white/purple","value":1}]
  }
}

let testCaseListKeys = Object.keys(myObj.TestCaseList[0]);

for(i=0; i < testCaseListKeys.length; i++){

    let tclKey = testCaseListKeys[i];

    console.log(tclKey + "\'s name = " + myObj.TestCaseList[0][tclKey].name);
    console.log(tclKey + "\'s value = " + myObj.TestCaseList[0][tclKey].value);

}

The console.logs are your output. The important values there are the myObj.TestCaseList[0][tclKey].name and the myObj.TestCaseList[0][tclKey].value


** UPDATE **

After answering the question Ananya asked how to do this same thing if the object had a different structure.

Updated Object:

var myObj2 = {
  "TestCaseList":
    [{
        "TC_1":{
          "name":"verifyloginpagedetails",
          "value":"2"}
      },
      {
          "TC_2":{
            "name":"verify registration page details",
            "value":"3" }
      }],
        "Summary":
        {
          "v":[ {"name":"over the ear headphones - white/purple","value":1}  ]
        }
  }

Updated JavaScript:

for(x=0;x<myObj2.TestCaseList.length;x++) {
  let testCaseListKeys = Object.keys(myObj2.TestCaseList[x]);

  for(i=0; i < testCaseListKeys.length; i++){
    let tclKey = testCaseListKeys[i];
    //console.log(tclKey);
    console.log(tclKey + "\'s name = " + myObj2.TestCaseList[x][tclKey].name);
    console.log(tclKey + "\'s value = " + myObj2.TestCaseList[x][tclKey].value);
  }
}

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