JSON Files: Nested Structures and Normalization
You'll learn how to load JSON data and flatten its nested structures into tabular formats suitable for analysis.
Why JSON Needs Special Handling
Unlike CSV files with their flat, row-and-column structure, JSON files often contain nested objects and arrays—like folders within folders. A single JSON record might have customer info at the top level, an array of orders one level down, and product details nested even deeper. Think of it like a Russian nesting doll: you need to unpack each layer to see everything inside.
Loading JSON Data
Python's json module and Pandas both offer ways to read JSON:
import json
import pandas as pd
# Method 1: Standard library
with open('data.json', 'r') as f:
data = json.load(f) # Returns Python dict/list
# Method 2: Directly to DataFrame
df = pd.read_json('data.json')
Use json.load() when you need to inspect or manipulate the structure first. Use pd.read_json() when the JSON is already flat and tabular.
Flattening with json_normalize()
When your JSON contains nested dictionaries or lists, json_normalize() is your ally:
from pandas import json_normalize
# Flatten nested structures into columns
df = json_normalize(data)
# Handle deeply nested data
df = json_normalize(data, record_path='orders',
meta=['customer_id', 'name'])
This function "unpacks" nested keys into dot-separated column names (like address.city) and can expand arrays into multiple rows.
Key Takeaway: Use json.load() or pd.read_json() to read JSON files, then apply json_normalize() to flatten nested structures into analysis-ready DataFrames with hierarchical column names.