SQL Server JSON Tutorial: FOR JSON, OPENJSON, JSON_VALUE and JSON_MODIFY

SQL Server 6 min readUpdated Nov 20, 2026
7 views0 comments

Every app, website and API sends data as JSON, so sooner or later it reaches your database. SQL Server can turn rows into JSON with FOR JSON, read values with ISJSON, JSON_VALUE and JSON_QUERY, turn JSON back into rows with OPENJSON, and change it with JSON_MODIFY.

Part of the SQL Server for Beginners series. This article is the written version of lesson 16 of the Bigland Network video course, with the same sample data. All examples were run in SQL Server Management Studio on a test instance. Run every example on a test server, not on production.

Watch lesson 16 as a video or open it on YouTube. The written lesson below follows the same steps and the same sample data.


Contents

1. JSON in 30 seconds

An object is written between curly braces and holds keys and values. Keys are text in double quotes. A value is text, a number, true, false or null. An array is an ordered list between square brackets. JSON is stored in an ordinary NVARCHAR column: there is no special type.

{ "name": "Amina", "salary": 4200, "skills": ["SQL", "Excel"], "boss": null }

2. Rows to JSON: FOR JSON PATH

SELECT TOP 3 Id, FirstName, Salary
FROM Employees
ORDER BY Id
FOR JSON PATH;

Result: one cell with one object per row.

[{"Id":1,"FirstName":"Amina","Salary":4200},{"Id":2,"FirstName":"Youssef","Salary":5200},{"Id":3,"FirstName":"Salma","Salary":3600}]

Add ROOT('employees') to wrap the array in a named object, a subquery that also ends with FOR JSON PATH to nest data, and INCLUDE_NULL_VALUES to keep NULL columns (Omar has no department):

SELECT TOP 2 FirstName, Salary FROM Employees ORDER BY Id
FOR JSON PATH, ROOT('employees');

SELECT d.DeptName,
       (SELECT e.FirstName FROM Employees e
        WHERE e.DeptId = d.DeptId
        FOR JSON PATH) AS staff
FROM Departments d
WHERE d.DeptId = 1
ORDER BY d.DeptId
FOR JSON PATH;

SELECT FirstName, DeptId FROM Employees
WHERE Id IN (5, 6) ORDER BY Id
FOR JSON PATH, INCLUDE_NULL_VALUES;

Results: {"employees":[{"FirstName":"Amina","Salary":4200},{"FirstName":"Youssef","Salary":5200}]}, then [{"DeptName":"Sales","staff":[{"FirstName":"Amina"},{"FirstName":"Salma"}]}], then [{"FirstName":"Nadia","DeptId":2},{"FirstName":"Omar","DeptId":null}]. Without INCLUDE_NULL_VALUES, the object of Omar would have no DeptId key at all.

3. Reading JSON: ISJSON, JSON_VALUE, JSON_QUERY

  • ISJSON(text) returns 1 for valid JSON and 0 for invalid text.
  • JSON_VALUE(text, path) returns one single value (text, number, true or false).
  • JSON_QUERY(text, path) returns a whole object or array, as JSON.

Paths: $ is the root, $.name a key, $.skills[0] the first array item (counting starts at 0), $.boss.city one level deeper. Paths are case sensitive.

DECLARE @j NVARCHAR(MAX) =
  N'{"name":"Amina","city":"Casablanca","skills":["SQL","Excel"]}';
SELECT ISJSON(@j) AS is_json,
       JSON_VALUE(@j, '$.name') AS name,
       JSON_VALUE(@j, '$.skills[0]') AS first_skill,
       ISJSON(N'{name:Amina}') AS bad_json;
is_json name first_skill bad_json
1 Amina SQL 0

The classic trap: JSON_VALUE on an array or an object returns NULL (no error). Use JSON_QUERY for those: JSON_QUERY(@j, '$.skills') returns ["SQL","Excel"], while JSON_VALUE(@j, '$.skills') returns NULL.

4. JSON to rows: OPENJSON

DECLARE @j NVARCHAR(MAX) =
  N'[{"name":"Amina","salary":4200},{"name":"Youssef","salary":5200}]';
SELECT * FROM OPENJSON(@j);
key value type
0 {“name”:”Amina”,”salary”:4200} 5
1 {“name”:”Youssef”,”salary”:5200} 5

Without a schema you get key, value and type (5 means object). Add a WITH clause to get real columns and data types:

DECLARE @j NVARCHAR(MAX) = N'[
 {"name":"Amina","salary":4200,"city":"Casablanca"},
 {"name":"Youssef","salary":5200,"city":"Rabat"},
 {"name":"Omar","salary":3900}]';
SELECT * FROM OPENJSON(@j)
WITH (name   NVARCHAR(50) '$.name',
      salary INT          '$.salary',
      city   NVARCHAR(50) '$.city');
name salary city
Amina 4200 Casablanca
Youssef 5200 Rabat
Omar 3900 NULL

A missing key gives NULL, not an error.

5. JSON_MODIFY and JSON in a column

DECLARE @j NVARCHAR(MAX) = N'{"name":"Amina","salary":4200}';
SELECT JSON_MODIFY(@j, '$.salary', 4500) AS raise,
       JSON_MODIFY(@j, '$.city', 'Casablanca') AS add_city;

Results: {"name":"Amina","salary":4500} and {"name":"Amina","salary":4200,"city":"Casablanca"}. The original text does not change: JSON_MODIFY returns new text, so save it with an UPDATE. To store JSON, use an NVARCHAR(MAX) column with a check constraint:

CREATE TABLE #Profiles (
  Id INT PRIMARY KEY,
  Info NVARCHAR(MAX) CHECK (ISJSON(Info) = 1));
INSERT INTO #Profiles VALUES
  (1, N'{"city":"Rabat","level":"senior"}'),
  (2, N'{"city":"Tangier","level":"junior"}');
SELECT Id, JSON_VALUE(Info, '$.city') AS city
FROM #Profiles
WHERE JSON_VALUE(Info, '$.level') = 'senior';

Result: one row, Id 1, city Rabat.

6. Mistakes and best practices

  • JSON_VALUE on an array or object gives NULL: use JSON_QUERY.
  • Paths are case sensitive: $.Name is not $.name.
  • Searching inside JSON text on a big table is slow.
  • Skipping ISJSON: one bad text breaks a query.
  • Putting everything in JSON when plain columns are better.
  • Best practice: normal columns for data you filter and join on, CHECK (ISJSON(col) = 1) on JSON columns, always a WITH schema in OPENJSON, JSON for flexible extras and for talking to apps, and test paths on a small sample first.

7. Practice

  • Return Youssef and Nadia (department 2) as JSON, with the root name it_team.
  • Read the salary from the JSON of Karim as a number, and add 100.
  • Turn a JSON array of two people into rows, and keep salaries above 3200.
-- 1
SELECT FirstName, Salary FROM Employees
WHERE DeptId = 2 ORDER BY Id
FOR JSON PATH, ROOT('it_team');

-- 2   (result 3200)
SELECT CAST(JSON_VALUE(N'{"name":"Karim","salary":3100}', '$.salary') AS INT) + 100 AS new_salary;

-- 3   (result: Salma 3600)
DECLARE @j NVARCHAR(MAX) =
  N'[{"name":"Salma","salary":3600},{"name":"Karim","salary":3100}]';
SELECT name, salary
FROM OPENJSON(@j)
WITH (name NVARCHAR(50) '$.name', salary INT '$.salary')
WHERE salary > 3200;

Frequently asked questions

Does SQL Server have a JSON data type?

In this lesson JSON is stored as text in an NVARCHAR column. Add a CHECK constraint with ISJSON to make sure only valid JSON gets in. Newer SQL Server versions also offer a native json type, but the functions shown here work the same way.

Why does JSON_VALUE return NULL?

Either the path does not exist (paths are case sensitive) or it points to an object or an array. Use JSON_QUERY for objects and arrays.

When should I use JSON instead of columns?

Use normal columns for data you filter, join and sort on. Use JSON for flexible extra attributes and for exchanging data with applications.

What to learn next

The next lesson, Lesson 17: Temporal Tables and Row History, covers temporal tables: SYSTEM_VERSIONING, AS OF and row history. Previous lesson: Lesson 15: Encryption and Auditing Basics.

The whole series

Sources and further reading

  • Microsoft Learn, Transact-SQL reference (Database Engine), on learn.microsoft.com: the official syntax for the statements used here.
  • Microsoft Learn, JSON data in SQL Server, FOR JSON, OPENJSON, JSON_VALUE, JSON_QUERY and JSON_MODIFY, for the full details of each feature.
Did you find this helpful?Tap the heart to let us know. It takes one second and no account.

More tutorials like this

Leave a Comment

Your email address will not be published. Required fields are marked *

Bigland Network IT

Practical tutorials for people who run, build and fix things: operating systems, databases, development, networking and security.

Topics

More

Site

© 2026 Bigland Network. All rights reserved.Always test commands on a non-production system first.
Scroll to Top