I am not sure if it's possible to check if a value of a specific key exists in an API GET Request Response. The below code does not work.
req = requests.get('MYAPI')
response = req.json()
if 'Tom' in res:
print('Welcome')
CodePudding user response:
req.json
actually returns a dictionary of items. To reveal the contents of a dictionary, you can use print
function like below:
my_dictionary = {'first_name': 'joe', 'last_name': 'biden', 'age': 14}
print(my_dictionary) # {'first_name': 'joe', 'last_name': 'biden', 'age': 14}
So whenever you want to find what is inside a dictionary, use print
.
Additionally, you could also use the fact that a dictionary is also an iterable object. This basically means, that you can iterate through the dictionary item by item. Here's a good answer that shows you how. Combine that with the fact that you want to find out whether at least one of those items is "Tom"
. Can you figure it out now?