How to Load JSON String into Pandas DataFrame?

JSON is a popular lightweight and easy-to-read format that is used to transmit data between a server and a client. In contrast, Pandas is a popular data analysis library used to work with data. To analyze and manipulate data from the internet, users must load the JSON data into Pandas’ DataFrame.

This post provides various ways to load JSON strings into Pandas DataFrame using the below-given contents:

Method 1: Using pandas.read_json() Function

The “pandas.read_json()” function is used to read JSON data or JSON data files. The path of the JSON file or a Python dictionary is passed to as a parameter to read it. 

To understand this method, let’s look at the following example:

Example 1: Load JSON String Into DataFrame

The below snippet shows the JSON file containing JSON strings:

Now, let’s look at the code to load JSON string into Pandas Dataframe:

Code:

import pandas
dataframe = pandas.read_json(r'C:\Users\p\Documents\program\example.json')
print(dataframe)
print(type(dataframe))
  • The “pd.read_json()” function reads JSON string by accepting the complete path of the json string file as a parameter.
  • The function retrieves a Pandas DataFrame.

Output:

The JSON string has been loaded into Pandas DataFrame.

Method 2: Using pandas.DataFrame.from_dict() Function

The “pd.DataFrame.from_dict()” function of Pandas is used to create a DataFrame by accepting the dictionary. The “json.loads()” and “pandas.DataFrame.from_dict()” function is used with the combination in the below code to load JSON string into Pandas DataFrame:

Code:

import pandas
import json
data = json.loads('{"Students":{"0":"Joseph","1":"Alex","2":"Lily","3":"Anna"},"Marks":{"0":55,"1":50,"2":90,"3":20}}')
print(pandas.DataFrame.from_dict(data, orient="index"))
  • The “json.loads()” function takes the JSON string as an argument and is stored in a variable named “data.”
  • The “pandas.DataFrame.from_dict()” function accepts the data and orient=“index” variable as an argument and retrieves the pandas DataFrame.

Output:

The input JSON string has been loaded into Pandas DataFrame.

Method 3: Using json_normalize() Function

The “pd.json_normalize()” function is used to flatten semi-structured JSON data into a flat table. The “json_normalize()” function returns a DataFrame. The below code is used to load JSON string into Pandas DataFrame:

Code:

import pandas
import json
data_frame = pandas.json_normalize(json.loads('{"name": "Joseph", "age": 22, "city": "New York"}'))
print(data_frame)
print(type(data_frame))
  • The module named “pandas” and “json” is imported.
  • The “json.loads()” function loads a JSON string into a Python dictionary.
  • The “json_normalize()” method converts the dictionary into a Pandas DataFrame.

Output:

The given JSON string has been loaded into Pandas DataFrame.

Conclusion

To load JSON strings into Pandas DataFrame, the “pd.read_json()”, “pd.DataFrame.from_dict()”, and “json_normalize()” functions are used in Python. The “pd.read_json()” function takes the JSON string file path as an argument and returns the Pandas DataFrame. Similarly, the “pd.DataFrame.from_dict()” and “json_normalize()” functions are used to load JSON strings into Pandas DataFrame. This guide presented various methods to load input JSON strings into DataFrame.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×