How to get data from JSON APIs with Python

Published on Aug. 22, 2023, 12:15 p.m.

To get data from JSON APIs with Python, you can use the requests library to make HTTP requests and the json library to parse the JSON data returned by the API.

Here’s an example of how to get data from a JSON API using Python:

import requests
import json

# Make a GET request to the API endpoint
response = requests.get('https://jsonplaceholder.typicode.com/posts')

# Parse the JSON data
data = json.loads(response.text)

# Print the data to the console
print(data)

In this example, we first import the requests and json libraries. Next, we use the requests.get() method to make a GET request to the API endpoint. We then use the json.loads() method to parse the JSON data returned by the API and store it in the data variable.

Finally, we print the data to the console using the print() function. You can also use the json.dump() method to write the JSON data to a file.

Note that in order to get data from a JSON API, you need to have the API endpoint URL and the proper authorization, if required by the API.

Tags: