1

I am trying to get the first id value in results array from that object I got from an API:

{
    "page" : 1,
    "results" : [{
            "adult" : false,
            "backdrop_path" : "/fLL6WfUXvdQee1fD4xuzNnWfVBk.jpg",
            "genre_ids" : [27, 9648, 80],
            "id" : 176,
            "original_language" : "en",
            "original_title" : "Saw",
            "overview" : "Obsessed with teaching his victims the value of life, a deranged, sadistic serial killer abducts the morally wayward. Once captured, they must face impossible choices in a horrific game of survival. The victims must fight to win their lives back, or die trying...",
            "release_date" : "2004-01-19",
            "poster_path" : "/dHYvIgsax8ZFgkz1OslE4V6Pnf5.jpg",
            "popularity" : 2.897462,
            "title" : "Saw",
            "video" : false,
            "vote_average" : 7.1,
            "vote_count" : 657
        }, {
            "adult" : false,
            "backdrop_path" : "/yKATxJtGY67cXdOmlbWwW6EgPqn.jpg",
            "genre_ids" : [27, 53, 80],
            "id" : 663,
            "original_language" : "en",
            "original_title" : "Saw IV",
            "overview" : "Jigsaw and his apprentice Amanda are dead. Now, upon the news of Detective Kerry's murder, two seasoned FBI profilers, Agent Strahm and Agent Perez, arrive in the terrified community to assist the veteran Detective Hoffman in sifting through Jigsaw's latest grisly remains and piecing together the puzzle. However, when SWAT Commander Rigg is abducted and thrust into a game, the last officer untouched by Jigsaw has but ninety minutes to overcome a series of demented traps and save an old friend...or face the deadly consequences.",
            "release_date" : "2007-10-25",
            "poster_path" : "/veApHw5ARGHWf3ptKf30rOGFY9n.jpg",
            "popularity" : 2.449196,
            "title" : "Saw IV",
            "video" : false,
            "vote_average" : 5.8,
            "vote_count" : 257
        }
    ]
}

Lets say my json object called json, What i tried so far is:

console.log(json.results[0].id);

and I got the following error: TypeError: Cannot read property '0' of undefined.

I can't figure out what seems to be the problem, Is there other way to get the first id value in JavaScript?

4
  • Is it still in string form? If so, use JSON.parse(json) to convert it to Object first. Commented Aug 29, 2015 at 9:01
  • if json is a JAVASCRIPT object, that should work - if it's a json STRING then you need to JSON.parse first Commented Aug 29, 2015 at 9:01
  • check this example Commented Aug 29, 2015 at 9:09
  • the problem is that it's not a string Commented Aug 29, 2015 at 9:12

2 Answers 2

3

Well, this should be so simple. If you keep JSON data in a Javascript variable, then the way you tried should work. See my version of it.

var jsonData = {
    "page" : 1,
    "results" : [{
            "adult" : false,
            "backdrop_path" : "/fLL6WfUXvdQee1fD4xuzNnWfVBk.jpg",
            "genre_ids" : [27, 9648, 80],
            "id" : 176,
            "original_language" : "en",
            "original_title" : "Saw",
            "overview" : "Obsessed with teaching his victims the value of life, a deranged, sadistic serial killer abducts the morally wayward. Once captured, they must face impossible choices in a horrific game of survival. The victims must fight to win their lives back, or die trying...",
            "release_date" : "2004-01-19",
            "poster_path" : "/dHYvIgsax8ZFgkz1OslE4V6Pnf5.jpg",
            "popularity" : 2.897462,
            "title" : "Saw",
            "video" : false,
            "vote_average" : 7.1,
            "vote_count" : 657
        }, {
            "adult" : false,
            "backdrop_path" : "/yKATxJtGY67cXdOmlbWwW6EgPqn.jpg",
            "genre_ids" : [27, 53, 80],
            "id" : 663,
            "original_language" : "en",
            "original_title" : "Saw IV",
            "overview" : "Jigsaw and his apprentice Amanda are dead. Now, upon the news of Detective Kerry's murder, two seasoned FBI profilers, Agent Strahm and Agent Perez, arrive in the terrified community to assist the veteran Detective Hoffman in sifting through Jigsaw's latest grisly remains and piecing together the puzzle. However, when SWAT Commander Rigg is abducted and thrust into a game, the last officer untouched by Jigsaw has but ninety minutes to overcome a series of demented traps and save an old friend...or face the deadly consequences.",
            "release_date" : "2007-10-25",
            "poster_path" : "/veApHw5ARGHWf3ptKf30rOGFY9n.jpg",
            "popularity" : 2.449196,
            "title" : "Saw IV",
            "video" : false,
            "vote_average" : 5.8,
            "vote_count" : 257
        }
    ]
};

console.log(jsonData.results[0].id);
console.log(jsonData.results[1].id);
Sign up to request clarification or add additional context in comments.

2 Comments

I know it should work but still get that TypeError: Cannot read property '0' of undefined.
Thank you all, You helped me figure out that the problem was with my get call. I don't know why the angular get call didn't work, but I tried the JQuery getJson method and could get to the values just like you said.
1

It is a better practice to check the datatype of the variable one is about to parse using typeOf json==='object', if it is a valid object then you can do further manipulations or else parse it using json=JSON.parse(json);

5 Comments

Tried that and got the following error: SyntaxError: Unexpected token o at Object.parse (native)
What is the result of type comparison ?
the result is true. What is the next step?
When I tried that : console.log(json.results); I got Undefined, and like I said before when I tried : console.log(json.results[0].id); I got the error: TypeError: Cannot read property '0' of undefined.
What is json in your case, is it the variable which holds the data you have posted in question ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.