0
0
Data Analysis Pythondata~5 mins

Reading JSON (read_json) in Data Analysis Python

Choose your learning style9 modes available
Introduction

We use read_json to easily load data stored in JSON format into a table-like structure for analysis.

You have data saved in a JSON file and want to analyze it in Python.
You receive data from a web API in JSON format and want to convert it to a table.
You want to quickly explore or clean JSON data using familiar table operations.
Syntax
Data Analysis Python
pandas.read_json(path_or_buf, orient=None, typ='frame', dtype=True, convert_axes=True, convert_dates=True, keep_default_dates=True, numpy=False, precise_float=False, date_unit=None, encoding=None, lines=False, chunksize=None, compression='infer')

path_or_buf is the file path or JSON string to read from.

orient tells pandas how the JSON data is organized (like 'records' or 'columns').

Examples
Reads a JSON file named data.json into a DataFrame.
Data Analysis Python
import pandas as pd

df = pd.read_json('data.json')
Reads JSON data from a string directly into a DataFrame.
Data Analysis Python
json_str = '[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]'
df = pd.read_json(json_str)
Specifies that the JSON file contains a list of records (rows).
Data Analysis Python
df = pd.read_json('data.json', orient='records')
Sample Program

This program reads a JSON string containing a list of people with their ages and converts it into a table. Then it prints the table.

Data Analysis Python
import pandas as pd

# JSON string with a list of people and their ages
json_data = '[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}, {"name": "Charlie", "age": 35}]'

# Read JSON string into DataFrame
people_df = pd.read_json(json_data)

# Print the DataFrame
print(people_df)
OutputSuccess
Important Notes

If your JSON data is large, consider using lines=True if the file has one JSON object per line.

Make sure the JSON structure matches the orient parameter to avoid errors.

Summary

read_json loads JSON data into a DataFrame for easy analysis.

You can read JSON from files or strings.

Adjust orient to match your JSON data layout.