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 six 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
|
For this part of the blog we're going to briefly abandon our Star Wars characters. Instead, consider this query:
-- list prolific directors in name order
SELECT
d.DirectorID,
d.FirstName,
d.FamilyName,
d.FullName,
d.DateOfBirth,
d.DateOfDeath,
d.Gender
FROM
Director as d
WHERE (
SELECT COUNT(*)
FROM Film as f
WHERE f.DirectorID = d.DirectorID
) > 20
ORDER BY
d.FamilyName
When you run this it lists out the most prolific directors from the Wise Owl movies database, which as it happens give these results:

Sadly, if unsurprisingly, all of the most prolific directors are male.
To turn this into JSON you just need to give your columns suitable names and add FOR JSON AUTO at the bottom:
-- list prolific directors in name order
SELECT
d.DirectorID,
d.FirstName as 'name.first',
d.FamilyName as 'name.last',
d.FullName as 'name.full',
d.DateOfBirth AS 'date.birth',
d.DateOfDeath 'date.death',
d.Gender
FROM
Director as d
WHERE (
SELECT COUNT(*)
FROM Film as f
WHERE f.DirectorID = d.DirectorID
) > 20
ORDER BY
d.FamilyName
FOR JSON AUTO
By giving the columns names SQL should automatically assign the columns into JSON objects as shown:
Object | Fields |
|---|---|
Root | DirectorId, Gender |
name | first, last, full |
date | birth, death |
Here's what this gives:

The JSON isn't easy to read in this format!
And here's the JSON copied into a separate file and indented:

Sadly, the JSON groups I mentioned above haven't been created.
To get the properly constructed JSON data, in your query change the last line to this:
ORDER BY
d.FamilyName
-- generate JSON section
FOR JSON PATH
This will then use the dots in your column aliases to generate sections:

Much better!
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.