Sometimes, we want to parse a JSON response from the Python requests library.
In this article, we’ll look at how to parse a JSON response from the Python requests library.
How to parse a JSON response from the Python requests library?
To parse a JSON response from the Python requests library, we can use the response.json
method.
For instance, we write:
import requests
response = requests.get('https://yesno.wtf/api')
json_data = response.json()
print(json_data)
We call requests.get
with a URL.
Then we call response.json
to return the JSON response as a dictionary.
And then we assign that to json_data
.
Therefore, json_data
is:
{'answer': 'yes', 'forced': False, 'image': 'https://yesno.wtf/assets/yes/8-2f93962e2ab24427df8589131da01a4d.gif'}
Conclusion
To parse a JSON response from the Python requests library, we can use the response.json
method.