Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
428 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Some other pages relevant to these blogs include:
You can also book hourly online consultancy for your time zone with one of our 7 expert trainers!
|
Using JSON in SQL Server, including the new SQL 2025 functions Part four of an eight-part series of blogs |
|---|
|
SQL Server 2025 introduces a new JSON data type and some new JSON functions - we thought we'd use this as an excuse to create this masterclass on working with JSON in SQL Server
|
In this blog
This page shows various ways in which you can get at data in this JSON file:

As always when working with JSON (or any other) data you need to be able to understand the structure of the data in order to make any sense of it in your SQL.
Recall that this JSON is stored in the PersonJson column in our tblPerson table:

We'll get the JSON from the PersonJson column in this table.
You can use the JSON_VALUE function to extract information from different nodes of a JSON file. For example:
-- show a couple of JSON columns
SELECT
p.PersonId,
JSON_VALUE(p.PersonJson, '$.count') AS NumberCharacters,
JSON_VALUE(p.PersonJson, '$.results') AS Results
FROM
tblPerson AS p
Here's what this would return:

The count object is correct, but the results one shows as null.
The reason the results are null is that the results object has nothing stored at its level, although it contains an array of characters at the next level down:

The results object is null, even though it contains an array of characters at a lower level of hierarchy.
As of SQL Server 2025 you can now get the array stored in a node using JSON_QUERY ... WITH ARRAY WRAPPER:
SELECT
p.PersonId,
JSON_VALUE(p.PersonJson, '$.count') AS NumberCharacters,
-- show the array stored in the "results" object
JSON_QUERY(p.PersonJson, '$.results' WITH ARRAY WRAPPER) AS Results
FROM
tblPerson AS p
Here's what this would show:

We now have the list of characters.
You can use the * key to get all of the items in an array, as this example shows:
-- create a variable to hold the JSON text
DECLARE @json_text NVARCHAR(MAX)
-- set this variable to be the contents of the file
SELECT @json_text = j.BulkColumn
FROM OPENROWSET(
BULK 'C:\wiseowl\person.json',
SINGLE_CLOB
) as j;
SELECT
-- show the array stored in the "results" object
JSON_QUERY(@json_text, '$.results[*].name') AS Results
This would give the names of all of the characters returned from the Star Wars API:

The list of all of the people returned.
If you only want certain people, here are some ideas for what you could put instead of the * in the SQL above:
What to put (path) | What it would return |
|---|---|
'$.results[0].name' | The first character's name |
'$.results[1 to 3].name' | The second, third and fourth characters' names |
However, the most useful way to get at data in JSON is probably still using the OPENJSON function, the subject of the next part of this blog.
| Parts of this blog |
|---|
|
Some other pages relevant to these blogs include:
You can also book hourly online consultancy for your time zone with one of our 7 expert trainers!
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2025. All Rights Reserved.