Sub topic 11
Handling missing meta keys with errors='ignore'
This chapter assumes you can already call pd.json_normalize with record_path and meta. You should also know that a key missing from a record normally becomes NaN when you flatten without record_path. Here you will see that meta behaves differently, and how errors changes that.
Why meta is stricter than plain flattening
Without record_path, json_normalize builds its columns from the keys it finds across all records. A record that lacks a key simply gets NaN in that column.
With record_path, you tell pandas which meta keys to copy onto every expanded row. If a record does not have one of those keys, pandas has nothing to copy. By default it treats this as an error and raises a KeyError.
The sample data below has a coupon field on only one of two orders.
import pandas as pd
orders_partial = [
{"order_id": 1, "coupon": "SAVE10", "items": [{"sku": "A1"}]},
{"order_id": 2, "items": [{"sku": "B2"}]},
]
try:
pd.json_normalize(
orders_partial,
record_path="items",
meta=["order_id", "coupon"],
)
except KeyError as e:
print("KeyError:", e)The error message tells you that the key is not always present and suggests errors='ignore'. That default is deliberate. A required field that disappears from your data is often a bug upstream, and a loud failure surfaces it.
Filling gaps with errors='ignore'
When a field is genuinely optional, pass errors="ignore". Pandas then puts NaN wherever a record lacks the meta key.
df = pd.json_normalize(
orders_partial,
record_path="items",
meta=["order_id", "coupon"],
errors="ignore",
)
print(df)
# sku order_id coupon
# 0 A1 1 SAVE10
# 1 B2 2 NaNEach row from the second order carries order_id 2 and a missing coupon. The errors parameter accepts only two values: "raise" (the default) and "ignore".
Nested meta paths
errors="ignore" also covers nested meta keys written as a list of keys. If any step of the path is missing, the result for that record is NaN.
orders = [
{"order_id": 100, "customer": {"name": "Ada"}, "items": [{"sku": "A1"}]},
{"order_id": 101, "items": [{"sku": "B2"}]},
]
df = pd.json_normalize(
orders,
record_path="items",
meta=["order_id", ["customer", "name"]],
errors="ignore",
)
print(df)
# sku order_id customer.name
# 0 A1 100 Ada
# 1 B2 101 NaNThe second order has no customer object at all, and the row still comes through.
What errors='ignore' does not cover
The setting applies to meta keys only. It does not apply to record_path. If a record lacks the list you named in record_path, pandas raises a KeyError whatever you pass for errors. The message says that every element of the data should have the path.
Two other cases need care.
- A record whose
record_pathlist is present but empty contributes no rows. That is not an error, but the parent record vanishes from the output. - A record with a missing
record_pathkey must be fixed before flattening, for example by supplying a default.
for order in orders_partial:
order.setdefault("items", [])Choosing between raise and ignore
Use the default when a missing key means the data is broken. Use errors="ignore" when absence is a normal state, such as an optional coupon. After flattening, you can count the gaps to confirm they are about the size you expect.
print(df["customer.name"].isna().sum())
# 1The tradeoff is that ignore hides typos. If you write "coupn" in meta, every row quietly gets NaN and no error appears. For that reason, check that the new column is not entirely empty.
df_check = pd.json_normalize(
orders_partial,
record_path="items",
meta=["order_id", "coupn"],
errors="ignore",
)
print(df_check["coupn"].isna().all())
# TrueExercise
You call pd.json_normalize(data, record_path="items", meta=["order_id", "coupon"]) and get a KeyError because some orders have no coupon. Another order has no items key at all. After you add errors="ignore", which problem is fixed and which one remains?
Show answer
The missing coupon is fixed. Those rows now get NaN in the coupon column. The order with no items key still raises a KeyError, because errors applies only to meta keys, not to record_path. You must add a default (such as an empty list) to that record before flattening.