Read JSON from an API into a Pandas DataFrame
Fetch JSON from a REST API with Python, normalize nested fields, and load the result into tabular format.
Introduction
Reading data from API (Application Programming Interface) is a common task in data analysis. When working with APIs, it is common to receive data in JSON (JavaScript Object Notation) format. JSON is a lightweight data interchange format which is easy for humans to read and write. It's also easily parsed and generated by machines. We will be using Python and it's libraries to read JSON data from APIs and convert them into tabular format after that.
Import Libraries
We will start our work by importing all the necessary libraries first. If requests module is not already installed in your machine you can install it by running any of below commands -
conda install requests(if you are using anaconda distribution)
pip install requests(without anaconda distribution)
import requests
import pandas as pd
from requests.auth import HTTPBasicAuthRead Data from API
There are some public APIs where data is accessible without any authentication. On the otherhand, data in business is mostly protected because of necessary privacy and security reasons. We will see how we can retrive data from APIs with/without authentication.
API with Authentication
We've written a function below for getting API data which is protected by username and password. We are using python's request and pandas module in this purpose.
def read_api_data_with_auth(url, username, password):
data = []
json_response = requests.get(url = url, auth = HTTPBasicAuth(username, password))
for i in json_response.json():
data.append(i)
df = pd.DataFrame(data)
print('Successfully read {0} rows from API: ({1})!'.format(df.shape[0], url))
return dfExample API-1:
Calling the function by passing API URL, username and password as parameter. The function sends a call to the API and retrived all the 168 rows present there.
df = read_api_data_with_auth('https://kc.humanitarianresponse.info/api/v1/data/965667', 'api_user', 'api_password')Below is a preview of top 3 rows returned by the function -
df.head(3)Example API-2:
Calling the function by passing API URL, username and password as parameter. The function sends a call to the API and retrived all the 112 rows present there.
df = read_api_data_with_auth('https://kc.kobotoolbox.org/api/v1/data/1282964', 'api_user', 'api_password')Below is a preview of top 3 rows returned by the function -
df.head(3)Example API-3:
Calling the function by passing API URL, username and password as parameter. The function sends a call to the API and retrived all the 4692 rows present there.
df = read_api_data_with_auth('https://kc.kobotoolbox.org/api/v1/data/867554', 'api_user', 'api_password')Below is a preview of top 3 rows returned by the function -
df.head(3)API without Authentication
We've written a function below for getting data from public APIs. We are using python's request and pandas module in this purpose as well.
def read_api_data(url):
data = []
json_response = requests.get(url = url)
for i in json_response.json():
data.append(i)
df = pd.DataFrame(data)
print('Successfully read {0} rows from API: ({1})!'.format(df.shape[0], url))
return dfExample API-4:
Calling the function by passing API URL as the only parameter. The function sends a call to the API and retrived all the 4566 rows present there.
df = read_api_data('http://universities.hipolabs.com/search?country=United+States')Below is a preview of top 3 rows returned by the function -
df.head(3)Example API-5:
Calling the function by passing API URL as the only parameter. The function sends a call to the API and retrived all the 10 rows present there.
df = read_api_data('https://hub.dummyapis.com/products?noofRecords=10&idStarts=1001¤cy=usd')Below is a preview of top 3 rows returned by the function -
df.head(3)So that's all for your introductory learning on reading JSON data from APIs and convert them into tabular format for further analysis. You may store them as tables into relational database for later analysis.