0
0
dbtdata~20 mins

Semi-structured data handling (JSON) in dbt - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSON Mastery in dbt
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
A[{'customer_id': null, 'purchase_amount': null}, {'customer_id': null, 'purchase_amount': null}, {'customer_id': null, 'purchase_amount': null}]
B[{'customer_id': '123'}, {'purchase_amount': 50.0}, {'customer_id': '456'}]
CSyntaxError: invalid JSON path syntax
D[{'customer_id': '123', 'purchase_amount': 50.0}, {'customer_id': '456', 'purchase_amount': 75.5}, {'customer_id': '789', 'purchase_amount': 20.0}]
Attempts:
2 left
💡 Hint
Remember that event_data:customer:id extracts the nested id inside customer in the JSON.
data_output
intermediate
2: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;
ATotal rows equal to the number of unique item names
BTotal rows equal to the number of orders
CTotal rows equal to the sum of all items in all orders
DTotal rows equal to zero due to syntax error
Attempts:
2 left
💡 Hint
Flattening a JSON array creates one row per array element.
🔧 Debug
advanced
2: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;
ASome rows have invalid JSON strings causing parse_json to fail
BThe function parse_json does not exist in dbt SQL
CThe column event_json is not a string type
DMissing alias for parse_json output causes syntax error
Attempts:
2 left
💡 Hint
Check if all JSON strings are valid and well-formed.
🧠 Conceptual
advanced
2: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?
AVARIANT - supports flexible nested JSON structures
BBOOLEAN - stores true/false event flags
CINTEGER - stores numeric event codes only
DVARCHAR - stores JSON as plain text without parsing
Attempts:
2 left
💡 Hint
Consider a data type that supports nested and flexible JSON.
🚀 Application
expert
3: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.
Aselect details.customer.id as customer_id, sum(details.purchase.amount) as total from sales group by customer_id;
Bselect details:customer:id as customer_id, sum(details:purchase:amount::float) as total from sales group by customer_id;
Cselect details['customer']['id'] as customer_id, sum(details['purchase']['amount']) as total from sales group by customer_id;
Dselect customer_id, sum(purchase_amount) from sales group by customer_id;
Attempts:
2 left
💡 Hint
Use colon notation and cast amounts to float for aggregation.