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.

1

1 Answer 1

2

That way is correct:

data.items[ 0 ].category[ 0 ].quality;
> [ Object, Object ]
Sign up to request clarification or add additional context in comments.

2 Comments

This is a sample JSON object the object is dynamically generated; it is of unknown depth.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.