0

I am new to python.

I have a array of JSON responds like this (from the API of theMovieDB), this is the first member of the array responds[0]:

{
page: 1,
results: [
{
poster_path: "/yVHNGrsIj3FNDg9lgseiZMlHbjJ.jpg",
adult: false,
overview: "The film centers on three brothers who, upon learning they only  have a few days left to live, set off to reverse a lifetime of mistakes. Hopper and Simmons are playing the brothers' father and uncle, respectively, while Caan is one of the brothers. Helfer is Caan's girlfriend, a woman with a dangerous past.",
release_date: "2010-09-17",
genre_ids: [
   35,
   18
 ],
id: 88057,
original_title: "A Beginner's Guide to Endings",
original_language: "en",
title: "A Beginner's Guide to Endings",
backdrop_path: "/dDYli8oDYOkgRp5f0kMqgQ1TSmt.jpg",
popularity: 1.006656,
vote_count: 4,
video: false,
vote_average: 5.75
}
],
total_results: 1,
total_pages: 1
}

I want to grab the id field, so I tried responds[0]['results']['id']- but it doesn't work! (responds[0]['results'] does work!) any idea why??

2
  • 2
    responds[0]['results'] looks like it contains an array, so you first have to extract the element to get the id field from. Commented Dec 8, 2015 at 16:10
  • how can I extract it? Commented Dec 8, 2015 at 16:14

2 Answers 2

4

The results key references a list of dictionaries; you'll have to use integers to index this:

responds[0]['results'][0]['id']

gives you the id key of the first dictionary in that list.

Sign up to request clarification or add additional context in comments.

Comments

0

As mentioned by Martijn responds[0]['results'] is a list and responds[0]['results'][0] is a dictionary. You can see the visual representation of the json array belowhere.
In order to access id you'll have to use: response['results'][0]['id']

Comments

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.