In MariaDB, JSON_ARRAY()
is a built-in function that returns a JSON array containing the specified values, provided as arguments.
Syntax
The syntax goes like this:
JSON_ARRAY([value[, value2] ...])
Where each of value
, value2
, ...
becomes an array element of the resulting array.
Arguments are optional, so it’s possible to create an empty array by not passing any arguments.
Example
Here’s an example to demonstrate the function.
SELECT JSON_ARRAY('Cat', 'Dog', 'Bird');
Result:
+----------------------------------+ | JSON_ARRAY('Cat', 'Dog', 'Bird') | +----------------------------------+ | ["Cat", "Dog", "Bird"] | +----------------------------------+
Here’s another one with numbers and null
:
SELECT JSON_ARRAY(0, 1, 2, 3, null);
Result:
+------------------------------+ | JSON_ARRAY(0, 1, 2, 3, null) | +------------------------------+ | [0, 1, 2, 3, null] | +------------------------------+
Escape Characters
The JSON_ARRAY()
function automatically escapes double quotes with a backslash.
Example:
SELECT JSON_ARRAY('The "right" time');
Result:
+--------------------------------+ | JSON_ARRAY('The "right" time') | +--------------------------------+ | ["The \"right\" time"] | +--------------------------------+
Empty Strings
Passing an empty string results in an empty string being added to the array:
SELECT JSON_ARRAY('','','');
Result:
+----------------------+ | JSON_ARRAY('','','') | +----------------------+ | ["", "", ""] | +----------------------+
Empty Arrays
As mentioned, it’s possible to create an empty array by not passing any arguments.
SELECT JSON_ARRAY();
Result:
+--------------+ | JSON_ARRAY() | +--------------+ | [] | +--------------+