Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Struggling with some JSON I've been provided. It's in this format:

[
[
    {
        "items": [
            {
                "id": "xxxxxxx",
                "name": "xxxxxxxxxx",
                "description": "xxxxxxxxxxx"
            },
            {
                "id": "xxxxxxx",
                "name": "xxxxxxxxxx",
                "description": "xxxxxxxxxxx"
            }
        ],
        "parentId": "xxxxxxxx",
        "title": "xxxxxxxxx",
        "type": "xxxxxxx"
    }
],
[
    {
        "items": [
            {
                "id": "xxxxxxx",
                "name": "xxxxxxxxxx",
                "description": "xxxxxxxxxxx"
            },
            {
                "id": "xxxxxxx",
                "name": "xxxxxxxxxx",
                "description": "xxxxxxxxxxx"
            }
        ],
        "parentId": "xxxxxxxx",
        "title": "xxxxxxxxx",
        "type": "xxxxxxx"
    }
]
]

So, I have an object named 'data'. If I stringify 'data' this is what I have above. Basically I have a parentId and I need to lookup that parentId in this JSON. I'm not used to this structure and I'm floundering trying to find a (relatively) simple solution. I'm used to having something like 'items' on the top level and I can drill down through that.

share|improve this question
Related: stackoverflow.com/a/11657379/1026459 – Travis J Jun 22 at 5:03
JSON is a string. This is a javascript object (in this case, array) – Jan Dvorak Jun 22 at 5:14

2 Answers

up vote 0 down vote accepted
for(var i=0;i<data.length;i++)
{if(data[i][0].parentId=='yourParent_id')
//stuff
}
share|improve this answer
Thanks, this is exactly what I needed and you were first to post. – DSel 2 days ago
you are welcome – Ankit 2 days ago

If you're open to using a library, you can do this in Underscore,

This:

_(data).findWhere({parentId: idToLookUp});

returns the object in an array where the parentId is equal to idToLookUp

Fiddle

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.