Home > other >  Filtering fields to be returned in API response (Python/programming newcomer)
Filtering fields to be returned in API response (Python/programming newcomer)

Time:07-18

Brand new Python beginner - not a programmer. I have a very simple question

import requests, json, pprint
from pprint import pprint

api_key='<mykey>'  #define API key
movie_title='The Godfather'  # define title
url = f'https://api.themoviedb.org/3/search/movie?api_key={api_key}&query={movie_title}'
results=requests.get(url)
j = results.json()
pprint(j)

For results I am getting more information than I need.

{'page': 1,
 'results': [{'adult': False,
              'backdrop_path': '/rSPw7tgCH9c6NqICZef4kZjFOQ5.jpg',
              'genre_ids': [18, 80],
              'id': 238,
              'original_language': 'en',
              'original_title': 'The Godfather',
              'overview': 'Spanning the years 1945 to 1955, a chronicle of the '....

Is there a way in Python to only be presented back with data from a few of the fields (for example only get back data for id, original_title and release_date?)

Thank you!

CodePudding user response:

As a list of tuples (without considering j["page"]):

[(elem["id"], elem["original_title"], elem["release_date"]) for elem in j["results"]]

CodePudding user response:

# As a result you've got a json response
# It is a python dictionary (a KEY-VALUE pairs of data)
# Python dictionary iz within curly braces {}
# To get out a value from dictionary you need to refer to a key 

myDict = {"Name": "Bob", "Age": 32, "Gender": "Male"}
print('My name is '   myDict["Name"]   ', I am '   str(myDict["Age"])   ' years old and my gender is '   myDict["Gender"]   '.')

# it will be printed:
#       My name is Bob, I am 32 years old and my gender is Male.

# Within your (incomplete) result there  is a list too
# A list is an array of values - the values are  indexed starting with 0
# If you want to get out the third letter from a list (C)

myList = ['A', 'B', 'C', 'D']
print(myList[2])

# it will be printed:
#       C

# Your result
j = {'page': 1, 'results': [{'adult': False, 'backdrop_path': '/rSPw7tgCH9c6NqICZef4kZjFOQ5.jpg', 'genre_ids': [18, 80], 'id': 238, 'original_language': 'en', 'original_title': 'The Godfather'}]}

# If you need the id then you can see that it is within the key "results" whose value is a list [] of dictionaries [{}]. ID is in the first element of the list and it's KEY is "id"
# To get it
print(j["results"][0]["id"])
# it will be printed:
#       238

print(j["results"][0]["original_title"])
# it will be printed:
#       The Godfather

# Try to read it from right to left like this 
#       Give me the value of KEY "original_title" - from the first element in list which (list) is the value of KEY "results" in the response
# Or from left to right
#       There is a KEY "results" holding a list in whose first member is another dictionary holding a name of the movie under the KEY "original_title" - get me that title
  • Related