What Is read_json() in Pandas?

read_json() is a Pandas function used to read JSON (JavaScript Object Notation) data and convert it into a DataFrame or Series. JSON is a widely used format for APIs, web services, and data exchange because it is lightweight and human-readable.

Why Use read_json()?

Using read_json() allows you to:

  • Load JSON files or API responses easily

  • Convert structured JSON data into tabular form

  • Handle different JSON orientations

  • Work with nested JSON data

  • Integrate API data into Pandas workflows

Basic Syntax

import pandas as pd df = pd.read_json("data.json")

This reads the JSON file and converts it into a DataFrame.

Simple JSON Example

Assume a file students.json:

[ {"Name": "Alice", "Age": 25, "City": "Delhi"}, {"Name": "Bob", "Age": 30, "City": "Mumbai"}, {"Name": "Charlie", "Age": 35, "City": "Chennai"} ]

Python
import pandas as pd df = pd.read_json("students.json") print(df) # Output: # Name Age City # 0 Alice 25 Delhi # 1 Bob 30 Mumbai # 2 Charlie 35 Chennai

Reading JSON from a URL

Pandas can read JSON data directly from a URL (commonly used with APIs):

Python
import pandas as pd
df = pd.read_json("https://example.com/data.json") print(df)

JSON Orientations

JSON data can be stored in different formats called orientations. Pandas supports multiple orientations using the orient parameter.

1. Records (Most Common)

[ {"Name": "Alice", "Age": 25}, {"Name": "Bob", "Age": 30} ]

Python
import pandas as pd df = pd.read_json("data.json", orient="records")

2. Columns

{ "Name": ["Alice", "Bob"], "Age": [25, 30] }

Python
import pandas as pd df = pd.read_json("data.json", orient="columns")

3. Index

{ "0": {"Name": "Alice", "Age": 25}, "1": {"Name": "Bob", "Age": 30} }

Python
import pandas as pd df = pd.read_json("data.json", orient="index") 

Reading Nested JSON

Nested JSON data (JSON inside JSON) can be flattened using json_normalize().

Python
import pandas as pd data = { "Name": ["Alice", "Bob"], "Details": [ {"Age": 25, "City": "Delhi"}, {"Age": 30, "City": "Mumbai"} ] } df = pd.json_normalize(data, "Details", meta=["Name"]) print(df) # Output: # Age City Name # 0 25 Delhi Alice # 1 30 Mumbai Bob

Handling Lines JSON (JSONL)

Some files store one JSON object per line.

df = pd.read_json("data.json", lines=True)

This is common in log files and streaming data.

Common read_json() Parameters

ParameterDescription
path_or_bufFile path or URL
orientJSON format structure
linesRead line-delimited JSON
dtypeSet data types
convert_datesParse dates automatically
encodingFile encoding

Key Points to Remember

  • read_json() loads JSON into DataFrames

  • Supports files, URLs, and strings

  • Handles multiple JSON formats

  • Nested JSON may require normalization

  • Commonly used for API data