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
This reads the JSON file and converts it into a DataFrame.
Simple JSON Example
Assume a file students.json:
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
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):
import pandas as pddf = 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)
Python import pandas as pd df = pd.read_json("data.json", orient="records")
import pandas as pd df = pd.read_json("data.json", orient="records") 2. Columns
Python import pandas as pd df = pd.read_json("data.json", orient="columns")
import pandas as pd df = pd.read_json("data.json", orient="columns") 3. Index
Python import pandas as pd df = pd.read_json("data.json", orient="index")
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().
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 BobHandling Lines JSON (JSONL)
Some files store one JSON object per line.
This is common in log files and streaming data.
Common read_json() Parameters
| Parameter | Description |
|---|---|
path_or_buf | File path or URL |
orient | JSON format structure |
lines | Read line-delimited JSON |
dtype | Set data types |
convert_dates | Parse dates automatically |
encoding | File 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