You can use the pandas.read_json() function to read JSON values from an API as Pandas DataFrame.
dataframe = pandas.read_json("api_endpoint_url")
Let us use the JSONPlaceholer – Fake Online REST API for Testing and Prototyping API to read JSON values as Pandas DataFrame. We’ll use the /posts endpoint which will return a JSON consisting of 100 dummy posts. You can view the API response by clicking this URL: https://jsonplaceholder.typicode.com/posts.
Now, we’ll use the pandas.read_json() function to read JSON values from the API discussed earlier as Pandas DataFrame.
# importing pandas to work with data import pandas as pd # URL of the API endpoint url = "https://jsonplaceholder.typicode.com/posts" # reading JSON values from the url as Pandas DataFrame df = pd.read_json(url) # displaying the top 5 rows of the dataframe df.head()
If you have any problems in reading JSON values from an API as pandas DataFrame, do let us know in the comment section below. You can also view our similar article: How To Read An Excel File As Pandas Dataframe in Python?