T-SQL Tutorial

SQL Server JSON_ARRAY


The JSON_ARRAY function in SQL Server is used to create a JSON array. It takes one or more arguments and returns a JSON array that contains those arguments as its elements. This function can be used to generate JSON arrays on the fly in SQL queries or to insert JSON arrays into a table column.


Syntax

The syntax of the JSON_ARRAY function is as follows:

JSON_ARRAY ( expression [ ,...n ] )

Where the expression parameter specifies the values to include in the JSON array, and the n parameter represents additional expressions to include in the array.


Example

For example, the following query creates a JSON array of numbers:

SELECT JSON_ARRAY(1, 2, 3, 4, 5);

The result of this query would be the following JSON array:

[1, 2, 3, 4, 5]

Similarly, you can also create a JSON array of strings using the JSON_ARRAY function:

SELECT JSON_ARRAY('John', 'Doe', '42');

The result of this query would be the following JSON array:

["John", "Doe", "42"]


In addition to simple values like numbers and strings, the JSON_ARRAY function can also include expressions that return more complex JSON data types like JSON objects or nested arrays.

Overall, the JSON_ARRAY function is a useful tool for creating JSON arrays in SQL Server, allowing for more flexible manipulation of JSON data within a database.