1

I have the following JSON object and using JS/jquery I am trying to list all boom entries that have a count greater than 0

using the following JS I can access all the boom's, but as the next property is unknown, how can I access the count?

id_unknown is not known in the example, as in, I don't know what the property name is so I can't do jsonData["foo"]["id"]["bar"]["id_unknown"]; I need to step over it almost and access everything inside it

obj = JSON.parse(results);
var prod = obj['foo'][id]['bar'];

     $.each(prod, function(i) {
               $.each(prod[i], function(boom) {
               console.log(boom);                  
            });
         });

JSON :

"foo":{  
  "id":{  
     "bar":{  
        "id_unknown":{  
           "boom":{  
              "count":9.0
           },
           "boom2":{  
              "count":48.0
           },
           "boom4":{  
              "count":103.0
           },
           "boom5":{  
              "count":0.0
          }
        }
     }
  }
}
Rhi
  • 29
  • 6

1 Answers1

0

Here you are:

var jsonData = {
    "foo": {
        "id": {
            "bar": {
                "id_unknown": {
                    "boom": {
                        "count": 9.0
                    },
                        "boom2": {
                        "count": 48.0
                    },
                        "boom4": {
                        "count": 103.0
                    },
                        "boom5": {
                        "count": 0.0
                    }
                }
            }
        }
    }
};
var data = jsonData["foo"]["id"]["bar"];
var unknownProperties = [];
$.each(data, function (k, e) {
    $.each(data[k], function (key, element) {
        alert(element["count"]);
    });
});

Hope this help.

hungndv
  • 2,121
  • 2
  • 19
  • 20
  • **id_unknown** is not known in the example, as in, I don't know what the property name is so I can't do jsonData["foo"]["id"]["bar"]["id_unknown"]; I need to step over it almost and access everything inside it – Rhi Jul 02 '15 at 15:47
  • I updated as your requirement. I run 2 each loops. – hungndv Jul 02 '15 at 15:51