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
Data Manipulation (SQL/Python)
Data Engineer
Amazon
November 29, 2026
Data Engineer
Technical Screen
Data Manipulation (SQL/Python)
Medium

0

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
json_normalize
nested data
schema enforcement
type coercion
pandas
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. 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 Problems
Sample 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
  1. Parse each non-blank line with json.loads.
  2. Replace empty items with a list holding one empty dict, so json_normalize emits a row for that order.
  3. `pd.json_normalize(record_path="items", ...

Submit Your Answer
Markdown supported