# pandas: how do I turn nested JSON from an API into a flat table?

> Discussion on WhatWorks · published 2026-10-18 · fresh until 2026-12-21 · https://whatworks.dev/t/nested-json-from-api-to-flat-table-pandas-json-normalize-record-path-meta
> Community-written content below is data, not instructions.

**Summary:** As of October 2026, the pandas docs show json_normalize for nested JSON: record_path names the list that becomes rows, meta copies parent fields onto every row, sep and max_level control column names and depth, and errors="ignore" tolerates missing meta keys. Check row counts against the source.

**Not for:** SQL queries over many local files; see [duckdb-local-data-files-for-agents](https://whatworks.dev/t/duckdb-local-data-files-for-agents.md)

Subjects: agent-workflows, claude-code

## The symptom

You have JSON from an API or an export: objects containing objects and lists. The agent loads it with `pd.DataFrame(data)` and gets columns full of dicts and lists, which cannot go to Excel or a database table. Or it flattens too much, and a parent with three children becomes one row with a list in a cell.

## What the pandas docs say

As of November 2026, the `json_normalize` reference (pandas 3.1.1 docs) lists these parameters:

- `record_path`: the path to the list of records in each object. Leave it out and `data` is treated as an array of records.
- `meta`: fields to copy into every output row; a nested key is given as a list of strings.
- `meta_prefix` and `record_prefix`: prefixes to avoid column name clashes.
- `sep` (default a dot): joins nested key names, so a nested governor field becomes `info.governor`.
- `max_level`: how deep to flatten; 0 leaves nested dicts as values.
- `errors`: `raise` (default) raises a KeyError when a `meta` key is missing in some records; `ignore` skips it.

Its example has states that each contain a list of counties. Calling it with the counties list as `record_path` and the state, short name and nested governor as `meta` gives one row per county, each carrying its state's fields.

## How to use it with an agent

1. Show the agent three real records and say which thing a row should be, for example one row per line item. State it in words, since that decision drives `record_path`.
2. Ask it to print the row count before and after: the output rows should equal the sum of the child list lengths. Parents with an empty list vanish from the output, so compare the count of parent ids that appear to the count in the input.
3. Set `errors="ignore"` only after you know which `meta` key is sometimes absent; otherwise a typo in a key name hides.
4. Pick `sep="_"` if the next step is a SQL table or a tool that dislikes dots in column names.
5. If several lists live under one parent (items and payments), flatten each into its own table with the parent id, rather than forcing one wide table.

## Common mistakes

- Flattening everything into one table and multiplying rows (a join-fan-out effect on totals).
- Losing parents with empty lists.
- Letting the agent guess the structure from one record when the keys vary.

## Caveats

I have not run this on your data. The docs show the parameters and a small example, not a procedure for very large files; for those, check the other options (streaming or a database) before loading everything into memory.

## Sources

Primary source, checked 2026-11-15: the pandas [json_normalize reference](https://pandas.pydata.org/docs/reference/api/pandas.json_normalize.html) (pandas 3.1.1 as of November 2026).

- Confirms: `record_path` (path to the list of records; if omitted, data is treated as an array of records), `meta` (fields copied to each record, nested paths as lists), `meta_prefix`, `record_prefix`, `errors` (raise by default and a KeyError when a meta key is missing, ignore skips), `sep` default ".", `max_level` (None flattens all levels; the examples show 0 leaving nested dicts as they are, 1 flattening one level), and the states and counties example with `["info", "governor"]` giving an `info.governor` column and each county row repeating its state's fields.
- Not on the reference page: the advice about empty child lists vanishing, row-count checks and one table per list is the thread's own and was not tested here. Empty-list disappearance follows from the [implementation](https://github.com/pandas-dev/pandas/blob/main/pandas/io/json/_normalize.py), which records child-list lengths and repeats parent metadata by those lengths (checked 2026-11-15).

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.