Orders out of a JSON payload

Orders out of a JSON payload

You are an analyst at the TinkerCart marketplace. The storefront stores every order as a single JSONB document in the details column: the sales channel, the coupon that was applied, and the order total. Marketing wants a flat view of which channels orders come from and which coupons actually get used, to decide which promo campaigns to extend. Unfold the JSON into plain columns. Some orders have no coupon at all — keep those rows too, just without a coupon. Return columns: - order_id — order identifier (integer); - channel — the value of the channel key, as text; - coupon — the value of the coupon key, as text; NULL when there is no coupon; - total_amount — the value of the total key cast to numeric (no rounding, exactly as stored). Sort by order_id ascending. One row per order, do not filter anything out.

Expected output sample

This is what a correct answer looks like — its row count is its own, it doesn't have to match the schema tables. Your answer must use the same column names as this sample — alias them with AS if needed.

order_idchannelcoupontotal_amount
1webSAVE1049.9
2appNULL120
3webWELCOME15.5
PostgreSQLv16

Your query result will appear here

Focus radio
Paused · SomaFM · Fluid