JSON_ARRAY (Transact-SQL)
Applies to: SQL Server 2022 (16.x) Azure SQL Database Azure SQL Managed Instance
Constructs JSON array text from zero or more expressions.
JSON_ARRAY ( [ <json_array_value> [,...n] ] [ <json_null_clause> ] )
<json_array_value> ::= value_expression
<json_null_clause> ::=
NULL ON NULL
| ABSENT ON NULL
Is an expression that defines the value of the element in the JSON array.
Can be used to control the behavior of JSON_OBJECT
function when value_expression is NULL. The option NULL ON NULL
converts the SQL NULL
value into a JSON null
value when generating the value of the element in the JSON array. The option ABSENT ON NULL
will omit the element in the JSON array if the value is NULL. The default setting for this option is ABSENT ON NULL
.
Returns a valid JSON array string of nvarchar(max) type.
For more info about what you see in the output of the JSON_ARRAY
function, see the following articles:
How FOR JSON converts SQL Server data types to JSON data types (SQL Server)
TheJSON_ARRAY
function uses the rules described in thisFOR JSON
article to convert SQL data types to JSON types in the JSON array output.How FOR JSON escapes special characters and control characters (SQL Server)
TheJSON_ARRAY
function escapes special characters and represents control characters in the JSON output as described in thisFOR JSON
article.
The following example returns an empty JSON array.
SELECT JSON_ARRAY();
Result
[]
The following example returns a JSON array with four elements.
SELECT JSON_ARRAY('a', 1, 'b', 2)
Result
["a",1,"b",2]
The following example returns a JSON array with three elements since one of the input values is NULL. Since the json_null_clause is omitted and the default for this option is ABSENT ON NULL, the NULL value in one of the inputs is not converted to a JSON null value.
SELECT JSON_ARRAY('a', 1, 'b', NULL)
Result
["a",1,"b"]
The following example returns a JSON array with four elements. The NULL ON NULL option is specified so that any SQL NULL value in the input will be converted to JSON null value in the JSON array.
SELECT JSON_ARRAY('a', 1, NULL, 2 NULL ON NULL)
Result
["a",1,null,2]
The following example returns a JSON array with two elements. One element contains a JSON string and another element contains a JSON object.
SELECT JSON_ARRAY('a', JSON_OBJECT('name':'value', 'type':1))
Result
["a",{"name":"value","type":1}]
The following example returns a JSON array with three elements. One element contains a JSON string, another element contains a JSON object & another element contains a JSON array.
SELECT JSON_ARRAY('a', JSON_OBJECT('name':'value', 'type':1), JSON_ARRAY(1, null, 2 NULL ON NULL))
Result
["a",{"name":"value","type":1},[1,null,2]]
The following example returns a JSON array with the inputs specified as variables or SQL expressions.
DECLARE @id_value nvarchar(64) = NEWID();
SELECT JSON_ARRAY(1, @id_value, (SELECT @@SPID));
Result
[1,"4BEA4F9F-D169-414F-AF99-9270FDB2EA62",55]
The following example returns a JSON array per row in the query.
SELECT s.session_id, JSON_ARRAY(s.host_name, s.program_name, s.client_interface_name)
FROM sys.dm_exec_sessions AS s
WHERE s.is_user_process = 1;
Result
session_id | info |
---|---|
52 | ["WIN16-VM","Microsoft SQL Server Management Studio - Query",".Net SqlClient Data Provider"] |
55 | ["WIN16-VM","Microsoft SQL Server Management Studio - Query",".Net SqlClient Data Provider"] |
56 | ["WIN19-VM","SQLServerCEIP",".Net SqlClient Data Provider"] |