Reputation: 51
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?
Upvotes: 1
Views: 15743
Reputation: 2117
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);
Upvotes: 3
Reputation: 36609
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);
Upvotes: 1