JSON in SQL Server
APIs speak JSON. SQL Server can return query results as JSON and read JSON documents into rows.
On this page · 5 min · Intermediate
On this page
- Learning objectives
- Explanation
- Step-by-step screens
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Nov 26, 2026
Related tutorials
Learning objectives#
- Return JSON with FOR JSON PATH
- Shred JSON into rows with OPENJSON
- Read single values with JSON_VALUE
- Index a value inside a JSON column
Explanation#
FOR JSON PATH turns a result set into a JSON array. Column aliases with dots (Customer.Name) create nested objects; a subquery with FOR JSON PATH creates nested arrays.
OPENJSON(@json) WITH (column type '$.path') turns a JSON array into rows — ideal for receiving a list of items from an API in one parameter.
JSON_VALUE(column, '$.path') reads one scalar, JSON_QUERY reads an object or array, and ISJSON checks validity (use it in a CHECK constraint). JSON is stored as NVARCHAR(MAX); SQL Server 2025 and Azure SQL also offer a native json type.
To filter on a JSON property quickly, add a computed column with JSON_VALUE and index it.
Step-by-step screens#
Drawings of SQL Server 2022 Setup and SQL Server Management Studio (SSMS 20) with the ShopDB sample database. The red numbers match the numbered actions under each picture; names on your computer will differ.
Loading pictures…
Code example: Return an order as JSON and import items from JSON#
-- 1. One order with its items as JSON
SELECT o.OrderId, o.OrderDate, c.Name AS [Customer.Name],
(SELECT p.Name, oi.Quantity
FROM OrderItems oi JOIN Products p ON p.ProductId = oi.ProductId
WHERE oi.OrderId = o.OrderId
FOR JSON PATH) AS Items
FROM Orders o JOIN Customers c ON c.CustomerId = o.CustomerId
WHERE o.OrderId = 1001
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER;
-- 2. Items posted by an API as JSON
DECLARE @items NVARCHAR(MAX) = N'[{"productId":3,"qty":2},{"productId":14,"qty":1}]';
INSERT OrderItems (OrderId, ProductId, Quantity, UnitPrice)
SELECT 1002, j.ProductId, j.Qty, p.Price
FROM OPENJSON(@items) WITH (ProductId INT '$.productId', Qty INT '$.qty') AS j
JOIN Products p ON p.ProductId = j.ProductId;
{"OrderId":1001,"OrderDate":"2026-11-20","Customer":{"Name":"Asha Verma"},
"Items":[{"Name":"Keyboard Wireless","Quantity":1},{"Name":"Mouse","Quantity":2}]}
(2 rows affected)This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- [Customer.Name] nests Name inside a Customer object.
- The subquery with FOR JSON PATH becomes an Items array.
- WITHOUT_ARRAY_WRAPPER returns one object instead of an array.
- OPENJSON WITH maps each JSON property to a typed column.
- The rows are inserted in one set-based statement.
Real-world use case#
An ASP.NET Core API passes a whole shopping cart as one JSON parameter to a stored procedure, which inserts all the items in a single round trip.
Common mistakes#
$.ProductId does not match "productId". JSON paths are case-sensitive.
JSON_VALUE in WHERE scans every row. Add an indexed computed column.
Best practices#
- Use FOR JSON PATH with aliases for shape
- OPENJSON WITH for typed import
- CHECK (ISJSON(col) = 1)
- Index computed JSON_VALUE columns
Remember this#
FOR JSON PATH = rows → JSON.
OPENJSON = JSON → rows.
JSON_VALUE reads one value.
Practice exercise#
Your task: Read the value of "city" from the JSON column Address in Customers.
Show solution
SELECT JSON_VALUE(Address, '$.city') AS City FROM Customers;
Mini quiz#
Which function turns a JSON array into rows?
Want more? Practise all SQL Server MCQs · SQL Server interview questions
My notes#
Saved in this browser.