Challenge - 5 Problems
JSON Mastery in dbt
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this dbt SQL code extracting JSON values?
Given a table
events with a column event_data storing JSON objects, what is the output of this query?dbt
select event_data:customer:id as customer_id, event_data:purchase:amount as purchase_amount from events limit 3;
Attempts:
2 left
💡 Hint
Remember that
event_data:customer:id extracts the nested id inside customer in the JSON.✗ Incorrect
The query extracts nested JSON fields correctly using colon notation. Option D shows the expected output with customer IDs and purchase amounts for three rows.
❓ data_output
intermediate2:00remaining
How many rows will this dbt model produce after flattening JSON arrays?
Consider a table
orders with a JSON column order_details containing an array of items. What is the number of rows returned by this query?dbt
select order_id, item.value:name as item_name from orders, lateral flatten(input => order_details:items) as item;
Attempts:
2 left
💡 Hint
Flattening a JSON array creates one row per array element.
✗ Incorrect
The lateral flatten expands each array element into its own row, so the total rows equal the total number of items across all orders.
🔧 Debug
advanced2:00remaining
Why does this dbt model fail with a JSON parsing error?
This dbt model tries to parse a JSON string column but fails. Identify the cause.
dbt
select parse_json(event_json) as event_data from raw_events where event_json is not null;
Attempts:
2 left
💡 Hint
Check if all JSON strings are valid and well-formed.
✗ Incorrect
parse_json fails if any string is malformed JSON. The error is due to invalid JSON in some rows.
🧠 Conceptual
advanced2:00remaining
Which JSON data type is best for storing semi-structured event logs in dbt?
You want to store event logs with varying fields and nested data. Which JSON data type should you use in your dbt model?
Attempts:
2 left
💡 Hint
Consider a data type that supports nested and flexible JSON.
✗ Incorrect
VARIANT type allows storing and querying nested JSON data efficiently in dbt.
🚀 Application
expert3:00remaining
How to extract nested JSON fields and aggregate purchase totals per customer in dbt?
Given a table
sales with a JSON column details containing nested customer and purchase info, write a dbt SQL query to get total purchase amount per customer.Attempts:
2 left
💡 Hint
Use colon notation and cast amounts to float for aggregation.
✗ Incorrect
Option B correctly extracts nested JSON fields using colon notation and casts amount to float for sum aggregation.