Categories
Python Answers

How to parse a JSON response from the Python requests library?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *