Pandas: How to parse a JSON file into a DataFrame

Updated: February 19, 2024 By: Guest Contributor Post a comment

Introduction

Pandas, a powerful and flexible open-source data analysis and manipulation library for Python, offers numerous functionalities for data processing. One common task is parsing JSON data into a pandas DataFrame, enabling easy data analysis and manipulation. JSON, JavaScript Object Notation, is a lightweight data-interchange format that is easy for humans to read and write and for machines to parse and generate. This tutorial will guide you through several approaches to convert a JSON file into a DataFrame, covering basic to advanced techniques with code examples.

You can use your own JSON data or download one of the following datasets to practice (save the downloaded one as data.json):

Basic JSON Parsing

First, ensure pandas is installed in your Python environment using pip install pandas. The simplest way to read a JSON file into a DataFrame is by using the pd.read_json() function.

import pandas as pd

# Load JSON data
json_path = 'data.json'
df = pd.read_json(json_path)

# Display the DataFrame
df.head()

This code snippet will read the JSON file located at ‘data.json’ and parse it into a DataFrame. The head() function displays the first few rows of the DataFrame for a quick overview of the data structure.

Handling Complex JSON Data

Real-world JSON data can be deeply nested, making direct parsing challenging. Pandas offers ways to handle such scenarios.

import pandas as pd
import json

# Open JSON file
with open('nested_data.json', 'r') as file:
    data = json.load(file)

# Normalize the data
from pandas.io.json import json_normalize
normalized_df = json_normalize(data, 'nested_key')

# Display the DataFrame
normalized_df.head()

This approach uses the json module to load the JSON into a Python dictionary, and json_normalize to flatten the data structure into a DataFrame. Replace ‘nested_key’ with the actual key that contains the array you wish to normalize.

Reading JSON Arrays into DataFrame

When dealing with a JSON file that contains an array of objects, we need a different approach to efficiently parse the file into a DataFrame.

import pandas as pd

# Assuming json_list.json contents are a list of objects
json_path = 'json_list.json'
df = pd.read_json(json_path)

# If the JSON is a string stored in a file, you might need to do:
# with open(json_path, 'r') as file:
#     data = file.read()
# df = pd.read_json(data)

# Display the DataFrame
df.head()

In some cases, you might need to read the JSON string from the file and then parse it, especially if the JSON data is stored as a string and not as an object array.

Advanced Techniques: Handling JSON with Different Orientations

Pandas’ read_json() function can handle JSON strings with different orientations, such as records, split, index, columns, and values. Understanding these orientations and how to use them can be very helpful.

# Assuming json_oriented.json contents are structured in 'split' orientation
import pandas as pd

json_path = 'json_oriented.json'
df = pd.read_json(json_path, orient='split')

# Display the DataFrame
df.head()

This example parses a JSON file with a ‘split’ orientation, where the data is divided into rows and columns. The orient parameter allows you to specify the expected JSON string format, enabling more controlled parsing.

Utilizing APIs to Fetch JSON Data

Often, the JSON data you’re looking to parse into a DataFrame comes from a web API. Here’s how you can fetch and parse JSON data from an API using requests library.

import requests
import pandas as pd

# Fetch JSON data from an API
url = 'https://api.example.com/data'
response = requests.get(url)
data = response.json()

# Convert to DataFrame
df = pd.DataFrame(data)

# Display the DataFrame
df.head()

This code sends a GET request to the specified URL, parses the JSON response into a Python dictionary, and finally converts that dictionary into a pandas DataFrame.

Conclusion

By leveraging pandas, Python’s premier data manipulation library, parsing JSON data into a DataFrame becomes a straightforward and flexible process. From simple JSON structures to complex and nested data, pandas provides the tools necessary to convert JSON into useful, analyzable data structures. This tutorial covered essential techniques and introduced advanced approaches for various JSON formats and sources, empowering you to efficiently work with JSON data in your data science projects.