Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through nested json object array

I have to iterate through json array object.

It has following structure.

var myJSONObject = {
    "abc": {
        "prod_1": [
            {"prod_ver" : "prod 1 ver 1"},
            {"prod_ver" : "prod 1 ver 2"},
        ],
        "prod_2": [
            {"prod_ver" : "prod 2 ver 1"},
            {"prod_ver" : "prod 2 ver 2"},
        ],
        "prod_3": [
            {"prod_ver" : "prod 3 ver 1"},
            {"prod_ver" : "prod 3 ver 2"},
        ]
    }
};

Basically what I am doing is prod_1 is name of a product and the list of versions that prod_1 has are populated inside it.

So now what i want is the name of the product as well as what versions it has.

The problem is there can be many products and many versions under that product. So i need a proper looping structure in javascript that can be generic to handle it.

It would be best if the loop stores product name in one var and it's version in another var as there are some checking i need to on product name.

If the json structure is wrong or a better json structure can be written the please suggest a right/better structure.

Please HELP

Thanks in advance.

like image 707
Saurabh Avatar asked Mar 07 '13 10:03

Saurabh


People also ask

How to iterate through all the nested keys in a JSON object?

You'll need to call it multiple times to iterate through all the nested keys. If you need to display the whole nested object, one option is to use a function to convert each object into a React component and pass it as an array: Another option is to use a package like json-query to help iterate through the nested JSON data.

Is it possible to iterate over a JSON object's products?

Since myJSONObject.abc contains a list of products it would make more sense to define the property abc as an array. Like this: Then you can iterate over the products and their versions using normal loops: You could take it slightly further and alter your JSON object's structure a bit to make it more easily understandable.

How to display the whole nested JSON object in react?

If you need to display the whole nested object, one option is to use a function to convert each object into a React component and pass it as an array: Another option is to use a package like json-query to help iterate through the nested JSON data.

How to loop through the keys of an object in JSON?

myJSONObject.abc is an object with keys like prod_1, prod_2, etc. You can loop through the keys of an object using for-in. So: Note that the order of the keys is not defined by the specification and will vary from JavaScript engine to JavaScript engine.


2 Answers

Since myJSONObject.abc contains a list of products it would make more sense to define the property abc as an array. Like this:

var myJSONObject = 
{
"abc":
    [
        [
            {"prod_ver" : "prod 1 ver 1"},
            {"prod_ver" : "prod 1 ver 2"},
        ],
        [
            {"prod_ver" : "prod 2 ver 1"},
            {"prod_ver" : "prod 2 ver 2"},
        ],
        [
            {"prod_ver" : "prod 3 ver 1"},
            {"prod_ver" : "prod 3 ver 2"},
        ]
    ]
};

Then you can iterate over the products and their versions using normal loops:

for(var i = 0; i < myJSONObject.abc.length; i++)
{
    var product = myJSONObject.abc[i];
    for(var j = 0; j < product.length; j++)
    {
        var version = product[j];
    }
}

You could take it slightly further and alter your JSON object's structure a bit to make it more easily understandable.

var catalog = 
{
    "products": [
        {
            "name": "prod 1",
            "versions": [
                "ver 1",
                "ver 2"
            ]
        },
        {
            "name": "prod 2",
            "versions": [
                "ver 1",
                "ver 2"
            ]
        }
    ]
};

for(var i = 0; i < catalog.products.length; i++)
{
    var product = catalog.products[i];
    var productName = product.name;
    for(var j = 0; j < product.versions.length; j++)
    {
        var version = product.versions[j];
    }
}
like image 112
Korijn Avatar answered Sep 29 '22 12:09

Korijn


myJSONObject.abc is an object with keys like prod_1, prod_2, etc. You can loop through the keys of an object using for-in. So:

var productName;
var productVersionArray;

for (productName in myJSONObject.abc) {
    productVersionArray = myJSONObject.abc[productName];
}

Note that the order of the keys is not defined by the specification and will vary from JavaScript engine to JavaScript engine. If you want to do them in a particular order, you have to get an array of them, sort it in the order you want, and then loop through that array. (In an ES5-enabled environment, you can get an array of the keys of an object from Object.keys(yourObject). But you'd need a shim for older browsers.)

Within that loop, you can loop through the array using a standard for loop:

for (versionIndex = 0; versionIndex < productVersionArray.length; ++versionIndex) {
    // Use `productVersionArray[versionIndex].prod_ver` here
}

Here's an example putting it all together:

(function() {

  var myJSONObject = 
    {
    "abc":
        {
            "prod_1": 
            [
                {"prod_ver" : "prod 1 ver 1"},
                {"prod_ver" : "prod 1 ver 2"}
            ],

            "prod_2": 
            [
                {"prod_ver" : "prod 2 ver 1"},
                {"prod_ver" : "prod 2 ver 2"}
            ],
            "prod_3": 
            [
                {"prod_ver" : "prod 3 ver 1"},
                {"prod_ver" : "prod 3 ver 2"}
            ]
        }
    };

  var productName;
  var productVersionArray;
  var versionIndex;

  for (productName in myJSONObject.abc) {
      productVersionArray = myJSONObject.abc[productName];
      display(productName + " has " + productVersionArray.length + " versions listed");
      for (versionIndex = 0; versionIndex < productVersionArray.length; ++versionIndex) {
        display("* " + productVersionArray[versionIndex].prod_ver);
      }
  }

  function display(msg) {
    var p = document.createElement('p');
    p.innerHTML = String(msg);
    document.body.appendChild(p);
  }

})();

Live Copy | Source

like image 43
T.J. Crowder Avatar answered Sep 29 '22 12:09

T.J. Crowder