SQL Server · Advanced Querying

JSON in SQL Server

APIs speak JSON. SQL Server can return query results as JSON and read JSON documents into rows.

Intermediate 5 min Lesson 35 of 44Examples run on SQL Server 2022 and Azure SQL Database.
Before you start:SELECTSubqueries
0/44 in SQL Server
On this page · 5 min · Intermediate

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#

SQLReturn an order as JSON and import items from JSON Illustrative
-- 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;
Expected output
{"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#

  1. [Customer.Name] nests Name inside a Customer object.
  2. The subquery with FOR JSON PATH becomes an Items array.
  3. WITHOUT_ARRAY_WRAPPER returns one object instead of an array.
  4. OPENJSON WITH maps each JSON property to a typed column.
  5. 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#

Case-sensitive paths

$.ProductId does not match "productId". JSON paths are case-sensitive.

Filtering on JSON without an index

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
SQL Illustrative
SELECT JSON_VALUE(Address, '$.city') AS City FROM Customers;

Mini quiz#

JSON in SQL Server quizMultiple choice Question 1 of 1

Which function turns a JSON array into rows?

Score so far: 0/0

Want more? Practise all SQL Server MCQs · SQL Server interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.