Javascript – Get value of key from a nested json

javascriptjqueryjsonnested

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.

Best Answer

That way is correct:

data.items[ 0 ].category[ 0 ].quality;
> [ Object, Object ]