🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

pd.read_json()

AI & DATA SCIENCE // pd-read-json

pd.read_json() parses JSON text (or a JSON file) into a DataFrame, converting nested key-value structures into rows and columns.

Syntax

pd.read_json(path_or_buf, orient=None)

Deep Dive Course

read_json() expects the JSON to represent tabular-ish data and uses the orient parameter to interpret its specific structure: 'records', a list of objects, each becoming one row, is one of the most common shapes, while 'columns', a dict mapping column names to dicts of index-value pairs, is another. JSON that's more deeply nested or irregularly structured than a simple table often needs preprocessing, or the separate pd.json_normalize() function, to flatten nested objects into proper DataFrame columns before or instead of a direct read_json() call.

1Understanding pd.read_json()

read_json() expects the JSON to represent tabular-ish data and uses the orient parameter to interpret its specific structure: 'records', a list of objects, each becoming one row, is one of the most common shapes, while 'columns', a dict mapping column names to dicts of index-value pairs, is another. JSON that's more deeply nested or irregularly structured than a simple table often needs preprocessing, or the separate pd.json_normalize() function, to flatten nested objects into proper DataFrame columns before or instead of a direct read_json() call.

💡

For JSON with nested objects or arrays inside each record, reach for pd.json_normalize() instead of, or in addition to, read_json() — it specifically flattens nested structures into flat columns, which plain read_json() doesn't attempt to do automatically.

editor.html
import pandas as pd

json_text = '[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]'
df = pd.read_json(json_text)
print(df)
localhost:3000

2Practical Example

Here is a real-world application of pd.read_json() showing how it is used in production Pandas code.

editor.html
import pandas as pd

data = [{"user": "alice", "address": {"city": "NYC", "zip": "10001"}}]
df = pd.json_normalize(data)
print(df.columns.tolist())
localhost:3000

3Best Practices

Follow these guidelines when working with pd.read_json():

1. Match the orient parameter to your JSON's actual shape, 'records' for a list of objects, which is the most common API response format, instead of relying on default detection

2. Use pd.json_normalize() for JSON with nested objects/arrays that need flattening into columns, rather than fighting read_json()'s simpler tabular assumptions

3. Validate the resulting DataFrame's shape and dtypes after reading, since inconsistent JSON records, missing keys, mixed types, can produce unexpected NaN values or object dtypes

⚠️

Tip: For JSON with nested objects or arrays inside each record, reach for pd.json_normalize() instead of, or in addition to, read_json() — it specifically flattens nested structures into flat columns, which plain read_json() doesn't attempt to do automatically.

editor.html
import pandas as pd

json_text = '[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]'
df = pd.read_json(json_text)
print(df)
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

json_text = '[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]'
df = pd.read_json(json_text)
print(df)
Example 02Advanced Example
import pandas as pd

data = [{"user": "alice", "address": {"city": "NYC", "zip": "10001"}}]
df = pd.json_normalize(data)
print(df.columns.tolist())

Best Practices

  • Match the orient parameter to your JSON's actual shape, 'records' for a list of objects, which is the most common API response format, instead of relying on default detection
  • Use pd.json_normalize() for JSON with nested objects/arrays that need flattening into columns, rather than fighting read_json()'s simpler tabular assumptions
  • Validate the resulting DataFrame's shape and dtypes after reading, since inconsistent JSON records, missing keys, mixed types, can produce unexpected NaN values or object dtypes

Interview Question

Why would pd.read_json() struggle with JSON that has deeply nested objects inside each record, and how does pd.json_normalize() help?

Hint: Think about what a DataFrame column can actually hold versus what a nested object represents.

A DataFrame is fundamentally a flat, two-dimensional grid of rows and columns, but a nested JSON object represents a hierarchical structure that doesn't map directly onto that flat shape — read_json() can technically load it, but the nested part typically ends up crammed into a single column as a raw dict or object, rather than being usefully split into separate columns. pd.json_normalize() is specifically designed to flatten that nested structure, turning nested keys into their own proper top-level columns using dot-separated names, which produces a genuinely flat, analysis-ready DataFrame instead of columns full of unprocessed nested objects.

Exercises

MediumPractice using pd.read_json() in a real scenario.
View Solution
import pandas as pd

json_text = '[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]'
df = pd.read_json(json_text)
print(df)

Frequently Asked Questions

Why would pd.read_json() struggle with JSON that has deeply nested objects inside each record, and how does pd.json_normalize() help?

A DataFrame is fundamentally a flat, two-dimensional grid of rows and columns, but a nested JSON object represents a hierarchical structure that doesn't map directly onto that flat shape — read_json() can technically load it, but the nested part typically ends up crammed into a single column as a raw dict or object, rather than being usefully split into separate columns. pd.json_normalize() is specifically designed to flatten that nested structure, turning nested keys into their own proper top-level columns using dot-separated names, which produces a genuinely flat, analysis-ready DataFrame instead of columns full of unprocessed nested objects.

Related Functions

pd-read-csvdf-to-jsondictionaries