Turn a JSON array into typed rows
Use OPENJSON with an explicit schema to read application JSON as relational columns.
What you are trying to do
An API sends a small task list as JSON. The receiving query needs typed identifiers, titles, and points before it can filter and sort them.
Work through it
- Read the JSON array as the row source.
- Match each WITH column to its JSON property and SQL type.
- Follow the filter after OPENJSON has produced relational rows.
- Check the two selected tasks and explain why task 2 is excluded.
Check your work
The result has exactly two rows: task 1, Cache review, 8 points, and task 3, Usage report, 12 points. The explicit schema converts each JSON property to its declared SQL type before the WHERE predicate runs.
Practice SQL
Open in SQL FlowSELECT TaskId, Title, Points
FROM OPENJSON(N'[
{"taskId": 1, "title": "Cache review", "points": 8},
{"taskId": 2, "title": "Queue alert", "points": 4},
{"taskId": 3, "title": "Usage report", "points": 12}
]')
WITH (
TaskId int '$.taskId',
Title nvarchar(40) '$.title',
Points int '$.points'
)
WHERE Points >= 7
ORDER BY TaskId;
Knowledge check
Commands in this lesson
Next lesson Ask a temporal table for an earlier row version