Advanced T-SQL and application patterns
Querying JSON in SQL Server
Create JSON from a product row and read a property with JSON_VALUE.
Lesson 82 of 84
Reading: about 2 min
Lesson + practice: about 20 min
Difficulty: Advanced
On this page4 sections
This lesson uses DDI_SQL_Training dataset v1.0.0 on SQL Server 2019 or later. Create JSON from a product row and read a property with JSON_VALUE.
Run the example
sql
SELECT p.ProductID, JSON_VALUE(j.Doc, '$.product.name') AS ProductName
FROM dbo.Products AS p
CROSS APPLY (
SELECT p.ProductName AS [product.name]
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
) AS j(Doc)
WHERE p.ProductID BETWEEN 1 AND 2
ORDER BY p.ProductID;Read it step by step
- FOR JSON PATH builds a small JSON document from each canonical product name.
- JSON_VALUE reads the scalar property at $.product.name.
- The outer ORDER BY makes the two-row display repeatable; SQL Server 2019 stores this JSON as text, not a native JSON column type.
Check the result
| ProductID | ProductName |
|---|---|
| 1 | Laptop Stand |
| 2 | USB-C Dock |
The round trip demonstrates path extraction without changing product data.
Practice
Extract a product name through JSON for ProductID 3.
Check your answer and expected result
Reveal answer
sql
SELECT p.ProductID, JSON_VALUE(j.Doc, '$.product.name') AS ProductName
FROM dbo.Products AS p
CROSS APPLY (
SELECT p.ProductName AS [product.name]
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
) AS j(Doc)
WHERE p.ProductID = 3;| ProductID | ProductName |
|---|---|
| 3 | Webcam |
The JSON scalar resolves to the same canonical product name.
Your progress
Reveal the check answer or reach the end to complete this lesson.