4

I have a (nested) data structure containing objects and arrays. How can I extract the information, i.e. access a specific or multiple values with help of a known key?

For example:

var data = {
    code: 42,
    items: [{
        id: 1,
        category: [{
            cId: 1,
            prodname: 'foo',
            quality: [{
                qId: 012,
                testName: 'micro'
            }, {
                qId: 013,
                testName: 'nano'
            }]
        }, {
            id: 2,
            prodname: 'bar'
        }]
    }]
};

How could I access the value of key quality?

Note: This is a sample JSON object the object is dynamically generated; it is of unknown depth.

Okky
  • 10,338
  • 15
  • 75
  • 122

1 Answers1

2

That way is correct:

data.items[ 0 ].category[ 0 ].quality;
> [ Object, Object ]
oleq
  • 15,697
  • 1
  • 38
  • 65
  • 1
    This is a sample JSON object the object is dynamically generated; it is of unknown depth. – Okky Jul 30 '13 at 07:57
  • If your JSON is of unknown depth or structure and you don't know where is your data, then the problem is definitely the logic that drives the process of creating such JSON. I'm sure that there's much more information than what you've revealed, which can be used to determine the structure of JSON. I'm also surprised that you're looking for a value of the key of unknown position in unknown tree. Why? I guess you should seriously re-think what you're trying to accomplish. Other than that, some tree traversal algorithms may be helpful if this is really what you want to do. – oleq Jul 30 '13 at 09:06