json_object and json_array Functions SQL Server 2022 Tutorial

By:   |   Updated: 2023-02-03   |   Comments (2)   |   Related: More > SQL Server 2022


Problem

Each day, JSON is becoming more popular and used in many systems. Is there a way to build your own JSON objects using T-SQL as opposed to Python?

Solution

In SQL Server 2022, a new function named JSON_OBJECT was introduced to construct JSON objects using T-SQL. Also, the JSON_ARRAY was included to create arrays in JSON. This tutorial will show how to work with these new functions.

The tutorial will include the following topics:

Requirements

  1. SQL Server 2022 installed
  2. SSMS installed
  3. For the last example, I will use the Adventureworks database.

JSON Introduction

JSON stands for JavaScript Object Notation. It is a format used for purposes similar to an XML file. It is used to interchange data using REST APIs, web services, and other applications.

JSON Syntax

A simple attribute in JSON is like this:

{"name":"Robert"}

Note that brace brackets are used. The name of the attribute is used with double quotes followed by a colon, and then the value of the attribute, which uses double quotes if it is a string.

If it is a number, it does not require double quotes as shown below.

{"age":57}

To handle null values, you use null.

{"Name":null}

The following example shows an object named actors that stores an actor's attributes (name, last name, age):

{"name":"Robert","lastname:Downey", "age":57}

Note: The attributes are separated by commas.

In JSON, you can also use arrays. Arrays use square brackets. The following example shows how to work with arrays. For numbers, you do not need double quotes around the values.

[1,2,4]

For arrays with strings, double quotes are required around the values.

["John","Bran","Sansa"]

For multiple records, we can use something like this:

[
{"name":"Robert","lastname":"Downey","age":57},
{"name":"Chris","lastname":"Hemsworth","age":39}
]

We used square brackets and each item (in this example, actor) is separated by commas.

For hierarchical data, you can have something like this. In this example, I will create a small hierarchical structure. The mother (Rhaenrya Targaryen from the House of the Dragon) has two children: Jacaerys and Lucerys.

diagram

The JSON syntax would be something like this:

{
"Name":"Rhaenyra",
"Lastname":"Targaryen",
"Children":[
   {
     "name":"Jacaerys",
     "lastname":"Velaryon",
     "age":20
   },
   {
     "name":"Lucerys",
     "lastname":"Velaryon",
     "age":18
   }
 ]
}

Now, we will learn how to build these JSON structures using JSON_OBJECT and JSON_ARRAY in T-SQL.

JSON_OBJECT Attribute Example with Strings

The following example creates the attribute name with the value Robert in JSON format.

SELECT JSON_OBJECT('name':'Robert') attribute
JSON_OBJECT attribute with strings

Note: The brace brackets are not used, and double quotes are replaced by single quotes.

JSON_OBJECT Attribute Example with Numbers

This example will use number attributes.

SELECT JSON_OBJECT('age':57) attribute
JSON_OBJECT attribute with numbers

Note that the numbers do not require quotes.

JSON_OBJECT Attribute Example with a Null Value

We can use null values.

SELECT JSON_OBJECT('name':null) attribute
JSON_OBJECT attribute with null values

The null values do not require quotes.

JSON_OBJECT Absent on Null Option

The absent on null option will not show the attribute if the value is null.

In this example, if the value is not null, then the value will be displayed:

SELECT JSON_OBJECT('name':2 ABSENT ON NULL) attribute
JSON_OBJECT attribute with absent of null

On the other hand, if a NULL value is included in the JSON, the value will not be displayed:

SELECT JSON_OBJECT('name':null ABSENT ON NULL) attribute
JSON_OBJECT attribute with absent of the null value

JSON_OBJECT with Multiple Attributes

When multiple attributes are used, they are separated by commas.

SELECT JSON_OBJECT('name':'Robert','lastname':'Downey','age':57) attributes
JSON_OBJECT attribute with multiple attribues

When multiple attributes are used, they are separated by commas.

JSON_ARRAY Example with Numbers

In this example, we will show how to create a JSON ARRAY using the JSON_ARRAY function.

SELECT JSON_ARRAY(1,2,4) myArray
JSON_ARRAY with numbers

JSON_ARRAY Example with Strings

The JSON_ARRAY function uses single quotes for strings.

SELECT JSON_ARRAY('John','Bran','Sansa') myArray
JSON_ARRAY with strings

JSON_OBJECT with an Array of Objects

The following example shows how to display multiple actors. For this example, we will create an array with two items. Each item has its attributes. In this example, the items are the actors Robert and Chris.

SELECT JSON_ARRAY
  (JSON_OBJECT('name':'Robert','lastname':'Downey','age':57), 
  (JSON_OBJECT('name':'Chris','lastname':'Hemsworth','age':39))) array
JSON_ARRAY with multiple objects

If you want to format the data to make it easier to read, you can use a JSON parser. There are many free tools online. With these tools, your data will be easier to read.

Format without the Parser

[{"name":"Robert","lastname":"Downey","age":57},{"name":"Chris","lastname":"Hemsworth","age":39}]

Format with the Parser

[
  {
    "name":"Robert",
    "lastname":"Downey",
    "age":57
  },
  {
    "name":"Chris",
    "lastname":"Hemsworth",
    "age":39
  }
]

Objects with Object Attributes

In some scenarios, you have hierarchical attributes. This example shows the mother and children hierarchical example explained before with Rhaenrya and her children.

SELECT JSON_OBJECT(
   'Name':'Rhaenyra',
   'Lastname':'Targaryen',
   'Children':
      JSON_ARRAY
         (JSON_OBJECT('name':'Jacaerys','lastname':'Velaryon', 'age':20),
         (JSON_OBJECT('name':'Lucerys','lastname':'Velaryon', 'age':18)))
) json

The output of this example is the following:

{
"Name":"Rhaenyra",
"Lastname":"Targaryen",
"Children":[
   {
     "name":"Jacaerys",
     "lastname":"Velaryon",
     "age":20
   },
   {
     "name":"Lucerys",
     "lastname":"Velaryon",
     "age":18
   }
 ]
}

Getting Information from Tables and Functions with JSON_OBJECT

Finally, we have an example using the Person.person table data from the Adventureworks database and the GETDATE function.

SELECT JSON_OBJECT('Firstname':FirstName,'Lastname':LastName,'Time':getdate()) attribute
FROM [Person].[Person]
JSON_OBJECT with table columns and functions
Next Steps
  • In this tutorial, we use the JSON_Object to create objects in JSON and the JSON_Array function to create JSON arrays. The syntax is simple, but if you have a complex and nested structure, you should indent your code to make it easier to use and read.
  • Try these new functions on your systems.


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Daniel Calbimonte Daniel Calbimonte is a Microsoft SQL Server MVP, Microsoft Certified Trainer and 6-time Microsoft Certified IT Professional. Daniel started his career in 2001 and has worked with SQL Server 6.0 to 2022. Daniel is a DBA as well as specializes in Business Intelligence (SSIS, SSAS, SSRS) technologies.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips


Article Last Updated: 2023-02-03

Comments For This Article




Monday, January 8, 2024 - 3:27:26 PM - Changxing Lu Back To Top (91843)
Need MSSQL 2019 workaround, please.

Monday, February 6, 2023 - 9:49:30 AM - Mike Back To Top (90885)
You may want to correct the syntax error in the example:

{"name":"Robert","lastname:Downey", "age":57}

According to your previous statement in the article, shouldn't it be:

{"name":"Robert","lastname":"Downey", "age":57}