Sub topic 8
Turning a list of records into rows with record_path
This chapter assumes you can read Python dictionaries and lists, build a DataFrame, and call pd.json_normalize on a list of dictionaries. You should also know that json_normalize turns nested objects into dotted column names and leaves lists as values inside a cell. Here you will expand those lists into rows.
The problem: one parent, many children
Many JSON documents have a parent record that holds a list of child records. An order contains line items. A shop contains daily sales. A user contains posts. With plain json_normalize, the list stays as a single Python list in one cell. That is awkward to filter, group, or sum.
record_path tells json_normalize which key holds the list to expand. Each element of that list becomes one row.
Your first record_path call
The sample data below has two orders. Each has a nested customer object and an items list.
import pandas as pd
orders = [
{
"order_id": 100,
"customer": {"id": 1, "name": "Ada"},
"items": [{"sku": "A1", "qty": 2}, {"sku": "B2", "qty": 1}],
},
{
"order_id": 101,
"customer": {"id": 2, "name": "Grace"},
"items": [{"sku": "A1", "qty": 5}],
},
]
df = pd.json_normalize(orders, record_path="items")
print(df.columns.tolist())
# ['sku', 'qty']
print(len(df))
# 3There are three rows, one for each element across both items lists. Two things to notice:
- The row count is the total number of list elements, not the number of orders.
- Only the contents of the list survive.
order_idandcustomerare gone, so you cannot tell which item belongs to which order. Themetaargument fixes this.
What record_path keeps and drops
The result contains the keys inside each list element, flattened the same way as in earlier chapters. Everything else in the parent is discarded unless you list it in meta.
| Parent content | Appears in the result? |
|---|---|
| Keys inside each list element | Yes, as columns |
| Nested objects inside a list element | Yes, flattened with the `sep` separator |
| Other keys of the parent record | No, unless named in `meta` |
| Other lists in the parent record | No, unless named in `meta` |
A list element that contains its own nested object is flattened like any other record:
catalog = [
{"shop": "North", "items": [{"sku": "A1", "price": {"amount": 3.5, "currency": "GBP"}}]},
]
df = pd.json_normalize(catalog, record_path="items")
print(df.columns.tolist())
# ['sku', 'price.amount', 'price.currency']Keeping parent fields with meta
Pass the parent keys you want as meta. Each value is repeated on every row produced from that parent. A nested parent field is written as a list of keys.
df = pd.json_normalize(
orders,
record_path="items",
meta=["order_id", ["customer", "name"]],
)
print(df.columns.tolist())
# ['sku', 'qty', 'order_id', 'customer.name']
print(df.to_dict("records"))
# [{'sku': 'A1', 'qty': 2, 'order_id': 100, 'customer.name': 'Ada'},
# {'sku': 'B2', 'qty': 1, 'order_id': 100, 'customer.name': 'Ada'},
# {'sku': 'A1', 'qty': 5, 'order_id': 101, 'customer.name': 'Grace'}]Order 100 has two items, so order_id 100 and Ada appear twice. The rows come out in the same order as the input: parents in sequence, and each parent's list elements in sequence.
The meta entries use two different forms, and they mean different things:
"order_id"is a top-level key.["customer", "name"]is a path: the keynameinside the objectcustomer. The column is namedcustomer.name, using thesepvalue (default.).
Do not write "customer.name" as a string. json_normalize treats it as a single literal key, which does not exist in the data.
A record_path that goes through nested objects
Sometimes the list is not directly under the parent. Give record_path a list of keys to walk down to it, in the same way as nested meta paths.
shops = [
{"shop": "North", "stock": {"updated": "2026-11-30", "items": [{"sku": "A1"}, {"sku": "B2"}]}},
{"shop": "South", "stock": {"updated": "2026-12-01", "items": [{"sku": "C3"}]}},
]
df = pd.json_normalize(
shops,
record_path=["stock", "items"],
meta=["shop", ["stock", "updated"]],
)
print(df.to_dict("records"))
# [{'sku': 'A1', 'shop': 'North', 'stock.updated': '2026-11-30'},
# {'sku': 'B2', 'shop': 'North', 'stock.updated': '2026-11-30'},
# {'sku': 'C3', 'shop': 'South', 'stock.updated': '2026-12-01'}]The path ["stock", "items"] means "take stock, then take items inside it". It must lead to a list of records at the end.
Edge cases
Empty lists drop the parent
A parent whose list is empty produces zero rows. Its meta values do not appear anywhere in the output.
gaps = [
{"order_id": 1, "items": [{"sku": "A1"}]},
{"order_id": 2, "items": []},
]
df = pd.json_normalize(gaps, record_path="items", meta=["order_id"])
print(df.to_dict("records"))
# [{'sku': 'A1', 'order_id': 1}]Order 2 has vanished. If you need to count parents, including those with no children, do not use the output of this call alone. Normalize the parents separately and merge, or keep a separate list of parent IDs.
A missing record_path key raises KeyError
If a record has no items key at all, json_normalize raises KeyError. The errors="ignore" option covers only missing meta keys, not a missing record path. Supply a default before normalizing:
data = [
{"order_id": 1, "items": [{"sku": "A1"}]},
{"order_id": 2},
]
try:
pd.json_normalize(data, record_path="items", meta=["order_id"])
except KeyError:
print("KeyError: a record has no 'items' key")
# KeyError: a record has no 'items' key
fixed = [{**o, "items": o.get("items") or []} for o in data]
df = pd.json_normalize(fixed, record_path="items", meta=["order_id"])
print(len(df))
# 1The path must point at a list
If record_path points at an object instead of a list, json_normalize raises an error because it has nothing to iterate over. Check the shape of the document with type(record["items"]) or by printing one record before you pick the path.
Lists of plain values
If the list holds scalars instead of dictionaries, the result has a single column named 0. Rename it right away.
posts = [{"id": 1, "tags": ["python", "pandas"]}, {"id": 2, "tags": ["json"]}]
df = pd.json_normalize(posts, record_path="tags", meta=["id"])
df = df.rename(columns={0: "tag"})
print(df.to_dict("records"))
# [{'tag': 'python', 'id': 1}, {'tag': 'pandas', 'id': 1}, {'tag': 'json', 'id': 2}]Name collisions
If a list element and the meta fields use the same key name, jsonnormalize raises a ValueError about conflicting metadata names. A common cause is items that carry their own id while the parent also has an id. The `recordprefix and meta_prefix` arguments solve this. A later chapter covers them.
The index is a fresh 0..n-1
The output index does not relate to the input position of the parent. Use a meta field such as order_id to join back to the parent, not the row index.
Choosing between meta and a separate table
Repeating parent fields on every row is convenient for analysis, but it multiplies data. If a parent has large fields, such as a long description, carrying them in meta copies them onto every child row. A leaner design is two tables: one flattened parent table with json_normalize(orders) and one child table with only a key in meta. You can then join them with merge when needed.
parents = pd.json_normalize(orders).drop(columns=["items"])
children = pd.json_normalize(orders, record_path="items", meta=["order_id"])
joined = children.merge(parents, on="order_id")
print(joined.columns.tolist())
# ['sku', 'qty', 'order_id', 'customer.id', 'customer.name']This gives the same information as a fully meta-based call, and it lets you keep the parent table around for questions that do not involve items.
Summary of the call shape
pd.json_normalize(
data,
record_path="items", # or ["stock", "items"] for a nested list
meta=["order_id", ["customer", "name"]],
)record_pathselects the list that becomes the rows.metaselects parent fields to copy onto each row.- A parent with an empty list contributes no rows.
- A parent without the
record_pathkey raisesKeyError.
Exercise
Given shops = [{"shop": "North", "region": {"name": "EU"}, "sales": [{"day": "Mon", "total": 10}, {"day": "Tue", "total": 12}]}, {"shop": "South", "region": {"name": "US"}, "sales": []}], what rows and columns does pd.jsonnormalize(shops, recordpath="sales", meta=["shop", ["region", "name"]]) produce?
Show answer
It produces 2 rows and the columns day, total, shop, region.name. Both rows come from North (Mon with total 10, Tue with total 12) and carry shop "North" and region.name "EU". South has an empty sales list, so it contributes no rows and does not appear in the result.
Exercise
A feed has some records with no "items" key. You call pd.jsonnormalize(data, recordpath="items", meta=["order_id"], errors="ignore") and still get an error. Why, and what is a fix?
Show answer
The errors="ignore" option only applies to missing meta keys. A record with no record_path key raises KeyError regardless. Fix it by supplying a default list before normalizing, for example data = [{**o, "items": o.get("items") or []} for o in data]. Those records then produce no rows instead of an error.