Order events arrive as parsed JSON, from two versions of the producer:
- •v1: line items under
"items", each with "sku", "qty", "price"; - •v2: line items under
"line_items", and the price is "unit_price".
Every event can also carry "order_id" and a "customer" object with "id" and "country". Any of these keys can be missing, and "customer" can be null.
Write flatten_orders(events) returning one flat dict per line item, with exactly the keys order_id, customer_id, country, sku, qty, unit_price:
- •read items from
"line_items" if the event has that key, otherwise from "items"; read the price from "unit_price" if the item has that key, otherwise from "price"; - •a missing value is
None (a null customer gives None for both customer fields); - •an order with no items (an empty list or no items key at all) still produces one row, with
sku, qty and unit_price set to None; - •rows keep input order: events in order, items in order within each event.
Python 3.13 in your browser — the standard library plus pandas and numpy; no pip installs.