How to access an array in a JSON object?

53,027

Solution 1

That's because your base object is an array as well.

console.log(dataJS[0].comments[0]);

I suspect that would work

Solution 2

the JSON you have coming back is actually an array itself, so...

dataJS[0].comments[0].created_at

will be 2011-02-09T14:42:42-08:00, etc...

Both dataJS and comments are arrays, and need indexes to access the appropriate elements.

Solution 3

console.log(dataJS);
console.log(dataJS[0].eee1);
console.log(dataJS[0].comments[0]);

Solution 4

The object being returned is itself an array, so to get to the first comment (as an example), this is how you would access it:

dataJS[0].comments[0]

Solution 5

Yes, as others have stated, the JSON is actually an Array (of a single Object). So you will need to reference an index.

Interestingly enough (to me), your result string does validate successfully as JSON. I assumed until now, that to be valid JSON, it had to be an Object (ie, {}).

Share:
53,027
AnApprentice
Author by

AnApprentice

working on Matter, a new way to gather professional feedback.

Updated on July 09, 2022

Comments

  • AnApprentice
    AnApprentice almost 2 years

    I have the following JSON object:

    [
        {
            "comments": [
                {
                    "created_at": "2011-02-09T14:42:42-08:00",
                    "thumb": "xxxxxxx",
                    "level": 1,
                    "id": 214,
                    "user_id": 41,
                    "parent_id": 213,
                    "content": "<p>xxxxxx</p>",
                    "full_name": "xx K"
                },
                {
                    "created_at": "2011-02-09T14:41:23-08:00",
                    "thumb": "xxxxxxxxxxxxx",
                    "level": 0,
                    "id": 213,
                    "user_id": 19,
                    "parent_id": null,
                    "content": "<p>this is another test</p>",
                    "full_name": "asd asd asd asd asd"
                }
            ],
            "eee1": "asdadsdas",
            "eee2": "bbbbb"
        }
    ]
    

    This is coming from a $.ajax request, in success I have....

    success: function (dataJS) {
        console.log(dataJS);
        console.log(dataJS[eee1]);
        console.log(dataJS.comments);
    }
    

    Problem is I can't get access to the items in the JSON object, even though dataJS does show correctly in the console. Ideas?

  • BoltClock
    BoltClock about 13 years
    Yes, jQuery evaluates the JSON.
  • jondavidjohn
    jondavidjohn about 13 years
    implied by $.ajax and the jquery tag
  • jondavidjohn
    jondavidjohn about 13 years
    No, it isn't built out of spec so no it's not 'incorrect', but what syntax are you wanting to use to access it?
  • AnApprentice
    AnApprentice about 13 years
    ok that's all I needed to know. Thank you! I will accept once stackoverflow lets me...
  • vbence
    vbence about 13 years
    I guessed $.ajax can be any framework, like $ is used generally by most of them $.ajax is a pretty basic combination, so it was not clear.
  • vbence
    vbence about 13 years
    @Brian: Most of the time i agree, but parsing without it is just a waste of resources. - And the server is a trusted source, your JS code comes from there anyway.
  • user113716
    user113716 about 13 years
    $.ajax does not necessarily imply that the data returned has been parsed.
  • Brian Driscoll
    Brian Driscoll about 13 years
    @vbence: hmm... you should probably read up on JSON.parse()
  • user113716
    user113716 about 13 years
  • user113716
    user113716 about 13 years
    @jondavidjohn: OP didn't specify what was meant by that. Could have been shown as a String.
  • Brian Driscoll
    Brian Driscoll about 13 years
    @patrick dw: yes, but only after it sanitizes the string. Thus, JSON.parse() > eval()
  • user113716
    user113716 about 13 years
    @Brian: Yeah, I know. That's really the point. There are proper and improper uses of eval.
  • vbence
    vbence about 13 years
    @Brian: The JSON library is still eval, it only checks for obvious signs of functions thru regex. As a rule of thumb I agree that using eval on unchecked data is bad, but it does not change the fact that is parsed by the eval function.
  • vbence
    vbence about 13 years
    As the post was kind of vague, I added a clause for security considerations. Hope no more misunderstandings.