dbt Analytics Engineering (50 Qs)•Part 3: Enterprise Scale, Performance & CI/CD Pipelines (Senior / Staff)
Working with Complex JSON Payloads in dbt: Macro patterns for cross-warehouse unnesting.
senior LevelMost AskedCode ExampleFormsPerformancePipes
In Simple Words:
Unnesting JSON arrays differs drastically across warehouses (Snowflake uses FLATTEN(), BigQuery uses CROSS JOIN UNNEST(), Postgres uses json_array_elements()).
- Cross-Database Macros: Write adapter-aware macros using
adapter.dispatchso your model code remains clean and portable:sqlSELECT order_id, {{ unnest_json_array('items', 'item_id') }} FROM {{ ref('stg_orders') }}