Flatten Nested Order JSON Into One Row per Order Line
Last updated: November 29, 2026
Quick Overview
Order events land in S3 as JSON lines where each order holds a nested list of items, and each item has a nested price object. Using pandas, flatten them into a table with one row per order line, keep orders that have no items, coerce types and compute a line total.
Amazon
November 29, 20260
0
0 solved
Order events land in S3 as JSON lines where each order holds a nested list of items, and each item has a nested price object. Using pandas, flatten them into a table with one row per order line, keep orders that have no items, coerce types and compute a line total.
Many Amazon teams receive nested JSON from service logs and event buses and must land it as tables for Athena or Redshift. This question checks whether you know json_normalize with record_path and meta, and whether you think about empty arrays, inconsistent types and missing keys rather than assuming a clean payload.
What the Interviewer Expects
- Use json_normalize with record_path for the item list and meta for order fields
- Preserve orders with empty item arrays instead of dropping them silently
- Coerce quantity and price types that arrive as strings
- Guarantee a stable output schema even when a key is missing from every record
- Skip blank lines robustly
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you handle a 50 GB file that does not fit in memory?
- How would you write the output as partitioned Parquet for Athena?
- What would you do with line totals in different currencies?
- How would you detect schema drift when the producer adds a new nested field?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
- Input: JSON lines, one order per line,
{"order_id", "marketplace", "items": [{"asin", "qty", "price": {"amount", "currency"}}]}. Blank lines happen at file boundaries. - Producers are inconsistent...
Approach
- Parse each non-blank line with
json.loads. - Replace empty
itemswith a list holding one empty dict, sojson_normalizeemits a row for that order. - `pd.json_normalize(record_path="items", ...