I'm a bit new to APIs and get requests and I'm using the Python requests
library.
I am getting data from an API like this:
url = "https://..."
headers = {"Accept":"application/json","Content-Type":"application/json","Authorization":"..."}
response = requests.get(url=url, headers=headers)
response = response.json()
My response.json()
looks like this:
[{'id': '1',
'fruit': 'apple',
'color': 'red'},
{'id': '2',
'fruit': 'orange',
'color': 'orange'},
{'id': '3',
'fruit': 'banana',
'color': 'yellow'},
...]
However, I only want specific records to be returned from my get request. E.g.
I have a list desired_fruits = ['apple', 'banana]
and only want the fruits
that are in this list. So my response.json
will look like:
[{'id': '1',
'fruit': 'apple',
'color': 'red'},
{'id': '3',
'fruit': 'banana',
'color': 'yellow'}]
How can I do this? Please help.
Because otherwise, I'll have to load in the whole thing and loop over the list of dictionaries to get desired records.