Data & BI

Verified against ChatGPT · 2026-08-11

Reshape a nested JSON payload into a flat schema without losing fields that only appear sometimes

Designs the exact flattening logic for a nested JSON API response into a tabular schema, explicitly accounting for fields that are optional, inconsistently typed, or only present in some records.

ChatGPT (GPT-5.1)3 fillable variables

The prompt

Ready to copy — highlighted parts are example details you can swap.

Design a flattening spec to turn the nested JSON sample below into a flat, tabular schema — the spec itself, not just an example of the flattened output, since this needs to run against every future payload from this API, not just the sample shown.

SAMPLE JSON PAYLOAD
{"order_id": "A1", "customer": {"id": 9, "email": "x@y.com"}, "items": [{"sku": "S1", "qty": 2}, {"sku": "S2", "qty": "1"}], "discount": null}

TARGET USE
One row per order-line-item, for loading into a revenue-by-SKU BI table

KNOWN VARIABILITY ACROSS RECORDS
The "discount" field is sometimes a nested object with amount/reason instead of null, and "items" can occasionally be an empty array for cancelled orders

Work through the payload structure and propose a flat schema: one row per One row per order-line-item, for loading into a revenue-by-SKU BI table's natural unit (a single event, order, or user — state which one you're choosing and why if it's not obvious from the payload). For each nested object or array, decide and state explicitly how it should be handled: flattened into prefixed columns (address.city, address.zip), exploded into multiple rows (one row per item in an array, with a clear rule for how the parent fields repeat across the exploded rows), or kept as a raw JSON string column if it's too variable to usefully flatten — and give the reasoning for which choice fits which field, not just the mechanical result. Explicitly handle fields that appear in some records and not others: state the exact behavior for a missing field (null vs. a specific default) rather than leaving it to whatever the flattening code happens to do by accident. Where a field's type is inconsistent across records in a way the sample hints at (a quantity field that's sometimes a string and sometimes a number, an array that's sometimes absent and sometimes empty), flag it as a normalization rule to build in, not something to discover later when the flattening script throws an error on a record shaped differently than the sample.

WHAT NOT TO DO
Do not just show one example of the sample payload flattened — the deliverable is the general rule, applicable to a payload you haven't seen, not a one-off transformation of the one example given. Do not silently choose "explode into rows" for every array without checking whether that specific array's cardinality actually calls for it versus a flattened summary column (e.g., a count or a comma-joined list).

OUTPUT FORMAT
1. Chosen row grain and why.
2. Field-by-field flattening rule: source path | target column(s) | flatten/explode/raw-JSON | missing-value behavior.
3. Type-inconsistency flags with the normalization rule for each.
4. One worked example showing the sample payload run through the spec.

Customize

Optional — swap in your own details for the highlighted parts above.

Why this works

A model asked to flatten a JSON sample will naturally just flatten the one example given, which produces something that looks complete but is actually just a demonstration — this prompt forces a shift from output to spec by explicitly requiring the reasoning for each structural choice (why this array explodes into rows versus that one collapses into a summary column), which is the part of the work that generalizes to payloads not yet seen, whereas the flattened example alone would not. Naming the row grain explicitly and asking for the reasoning behind it addresses a subtle but common design error in JSON flattening: choosing the wrong natural unit (one row per order instead of one row per line-item, for instance) doesn't cause an error, it just silently produces a schema that can't answer the questions the downstream use actually needs answered, and that mistake is invisible until someone tries to run an aggregate query against it later. Requiring explicit missing-value behavior per field, rather than leaving it to "whatever the flattening code does," matters because in most real flattening implementations a genuinely absent field and a field present with a null value can end up looking identical in the output table unless the spec states which is which, and that distinction can matter for a downstream count or an aggregate that treats "never had a discount" differently from "had a $0 discount." Flagging type inconsistency (a quantity field that's sometimes string, sometimes number) as a spec-level rule rather than an implementation afterthought is what prevents the actual, common production failure mode of a flattening script working fine against the sample and then throwing a type error the first time a real-world payload deviates from it — building the normalization rule into the spec up front means the eventual code handles that case by design, not by a patch added after the first crash.

What you get back

Row grain: one row per order line-item (order_id + item index), because target_use aggregates revenue by SKU. customer.email → customer_email | flatten | required, no missing-value case observed in sample. items[].qty → item_qty | explode (one row per item) | type-inconsistency flag: cast to integer, treating string "1" and numeric 2 identically; log any non-numeric value rather than silently coercing to 0. discount → discount_amount, discount_reason | flatten with null-safe defaults (0, null) | known variability flag: object shape sometimes differs from sample — confirm both shapes before finalizing.

Verified against

ChatGPT GPT-5.1 · 2026-08-11

Changelog

  • 2026-08-11 Initial publish, verified against ChatGPT GPT-5.1.

Need this built into your business?

If a prompt isn't enough — what Scult builds, built and maintained for you — that's Scult's day job.

EXPLORE WHAT SCULT BUILDS
All Data & BI prompts

Check your AI visibility

One URL in, a 0–100 score and the exact fixes out.

RUN THE CHECK

Browse all the tools

15 tools across six categories
13 of them never send your data anywhere

Free · No signup · No trial clock

SEE THE DIRECTORY