pythonbeginner

Flatten Nested JSON with pandas

Use pd.json_normalize to flatten deeply nested API responses into a flat DataFrame.

python
import pandas as pd

data = [
    {'id':1,'user':{'name':'Alice','address':{'city':'NY','zip':'10001'}},'score':95},
    {'id':2,'user':{'name':'Bob','address':{'city':'LA','zip':'90001'}},'score':87},
]

df = pd.json_normalize(data, sep='_')
print(df.columns.tolist())
print(df)

Use Cases

  • API response flattening
  • JSON ETL
  • nested data processing

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.