In this post, you will learn how to read JSON from a URL in Python using Pandas. For example, we may want to get data from a website or an API and load it directly into a Pandas dataframe.
In a previous post, we looked at how to read and write JSON files using Python and Pandas. Here, we will focus on reading JSON from a URL using pd.read_json(). We will go through the Python code and see how to load the data into a dataframe.
How to use Pandas to Read JSON from URL
First, let us look at a simple example of using Pandas to read JSON from a URL.
import pandas as pd
# URL containing JSON data
url = "http://api.open-notify.org/astros.json"
# Read JSON data from URL into a DataFrame
df = pd.read_json(url)
# Display the dataframe
print(df)Code language: PHP (php)
In the code chunk above, we start by importing the Pandas library. The URL variable contains the web address where the JSON data is hosted. The pd.read_json(url) function is then used to read the JSON data from the URL and load it into a Pandas DataFrame, which is a two-dimensional labeled data structure with columns of potentially different types. Finally, print(df) displays the DataFrame, allowing us to see the imported data in tabular format.

Now that we have seen a basic example, let us learn more about the parameters of the pd.read_json() method to understand how we can customize the reading process.
Parameters of the read_json Method
The pd.read_json() method has several parameters that allow you to fine-tune how the JSON data is read and converted into a dataframe. Here is an overview of the most important parameters:
path_or_buf: The string containing the URL or the path to the JSON file. This is the source of the JSON data that will be read.orient: Defines the expected JSON string format. Default is ‘columns’. This parameter specifies the orientation of the JSON data. Other options include ‘split’, ‘records’, ‘index’, and ‘values’.typ: Specifies the type of object to be returned. Default is ‘frame’. This parameter can be set to ‘series’ if you want to return a Series instead of a DataFrame.dtype: Determines whether to infer types of objects. Default is ‘None’. This parameter can be used to specify the data type for each column.convert_axes: Whether to convert the axes to another type. Default is ‘True’. This parameter allows you to convert the axes to a specified data type.convert_dates: List of columns to convert to dates. Default is ‘True’. This parameter can be used to specify which columns should be parsed as dates.keep_default_dates: Whether to include default date parsers. Default is ‘True’. This parameter determines whether to use the default date parsers provided by Pandas.precise_float: Whether to use a high precision floating point converter. Default is ‘False’. This parameter can be set to ‘True’ if you need high precision for float values.date_unit: Unit for encoding datetime. Default is ‘None’. This parameter can be used to specify the time unit for encoding datetime objects.encoding: Specifies the encoding to be used. Default is ‘utf-8’. This parameter determines the encoding for reading the JSON data.lines: Whether to read the JSON file as a JSON object per line. Default is ‘False’. This parameter can be set to ‘True’ if the JSON data is in a line-delimited format.
With these parameters allows you to better control how JSON data is read and processed, enabling you to tailor the DataFrame to your needs.
Summary
In this post, you learned how to read JSON from a URL in Python using Pandas. We looked at how to use pd.read_json() to load JSON data directly into a dataframe. We also went through some of the parameters you can use when reading JSON data.
As you have seen, reading JSON from a URL can be useful when we want to work with data from websites or APIs. However, some JSON files contain nested data, which may require additional steps before we can work with the data in Pandas. We will look at how to handle nested JSON in a future post.
I hope you found this tutorial helpful. If you have any questions or suggestions, please leave a comment below. Also, if you found this post useful, feel free to share it with others who might be interested in working with JSON data in Python.
Resources
Here are some other reading data-related tutorials: