SQLite JSON_ARRAY function

The json_array() function in SQLite is used to create a new JSON array. This function takes zero or more arguments, which will be the elements of the JSON array. The arguments can be of any data type, including NULL.

Syntax

Here’s the syntax for the json_array() function:

json_array(value1, value2, ..., valueN)

The json_array() function returns a new JSON array with the given values. If there are no arguments, it returns an empty JSON array.

Examples

Let’s look at some examples to better understand how to use the json_array() function.

Example 1: Creating a JSON array with values

SELECT json_array('apple', 'orange', 'banana');

Output:["apple", "orange", "banana"]

Example 2: Creating a JSON array with NULL values

SELECT json_array(NULL, 'apple', NULL, 'orange', NULL, 'banana');

Output: [null, "apple", null, "orange", null, "banana"]

Example 3: Creating an empty JSON array

SELECT json_array();

Output: []

You can also use the json_array() function with other JSON functions in SQLite to manipulate JSON data. For example, you can use the json_insert() function to insert a new element into a JSON array.

In summary, the json_array() function is a useful tool for creating new JSON arrays in SQLite. It’s simple to use and can accept any data type, including NULL.