2

I'm trying to iterate through a JSON object I have, in order to retrieve a list of elements.

The JSON code I am sifting through is really complex, but here is small snippet. It basically shows what I'm looking for.

{
  "ProductsList": [
    {
      "ProductInfo": {
        "Brand": "LG",
        "p_product_barcode": "048231014731",
        "p_product_bullets_json": {
          "Value": [
            "4.3 cubic foot ",
            "Recognized",
            "Customize your washer",
            "6Motion Technology ",
            "Troubleshoot quickly",
            "meow",
            "who",
            "special"
          ]
        }
    }]
}

I'm trying to get the list of values from "Value", which is inside of "p_product_bullets_json". I want get all of the elements.

So far I have this, but all I'm getting is an empty list.

function getLists(obj, key) {

    // Empty object array
    var objects = [];

    // Searches through the JSON code to find the given key
    for (var k in obj) {

        // If there are still leafs left, then keep searching
        if (!obj.hasOwnProperty(k)) continue;

        // If the leaf doesn't match the key, try again (recursion)
        if (typeof obj[k] == 'object') {

            objects = objects.concat(getValues(obj[k], key));

        // If the leaf does match the key, then push that value onto the array
        } else if (k == key) {

            $.each(obj[k], function(i, val) {

                console.log('Key: ' + i + '  Val: ' + val)

            });
        }
    }

    return objects;
}

I would just look through every Key for "Value", but this name isn't unique and there are other Key's with the same name in other places.

Any help would be greatly appreciated, thank you!

3
  • 1
    What is getValues function? Commented Apr 13, 2015 at 15:37
  • Wouldn't it be easier to parse the JSON into a javascript object and access it the way you want? Commented Apr 13, 2015 at 15:38
  • What do you mean? The JSON has already been parsed, sorry I probably should've made that clear. In my getLists function, it's called obj. I was just showing an example of the thing I'm trying to find in my JSON. Commented Apr 13, 2015 at 16:07

1 Answer 1

1

You need to have one case in which you return an array from that function, try this:

var input = {
  "ProductsList": [
    {
      "ProductInfo": {
        "Brand": "LG",
        "p_product_barcode": "048231014731",
        "p_product_bullets_json": {
          "Value": [
            "4.3 cubic foot ",
            "Recognized",
            "Customize your washer",
            "6Motion Technology ",
            "Troubleshoot quickly",
            "meow",
            "who",
            "special"
          ]
        }
      }
    }]
};
function getLists(obj, key) {

    var objects = [];

    for (var k in obj) {

        if (!obj.hasOwnProperty(k)) continue;
        if (k == key) {
            if (obj[key] instanceof Array) {
                return obj[key]
            }
        } else if (typeof obj[k] == 'object') {

            objects = objects.concat(getLists(obj[k], key));
        }
    }

    return objects;
}
document.getElementsByTagName('div')[0].innerHTML = JSON.stringify(getLists(input, 'Value'));
    
<div></div>

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

3 Comments

For some reason when I try this in my code I keep getting an empty list.
@bleakthecat420 can you create jsFiddle with your real JSON?
Nvm! I got it working! It didn't capitalize something. Thank you so much! You just saved my life! :D

Your Answer

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