MySQL JSON_ARRAY() Function
In MySQL, the JSON_ARRAY() function returns a JSON array that includes all parameters.
JSON_ARRAY() Syntax
Here is the syntax of the MySQL JSON_ARRAY() function:
JSON_ARRAY(value1, value2, ...)
Parameters
value1, value2, ...- Optional. Some values that will be elmeents in a JSON array.
Return value
The JSON_ARRAY() function evaluates all the parameters and returns a JSON array including all the parameters.
There may be some conversions happening here:
TRUEis converted totrue.FALSEis converted tofalse.NULLis converted tonull.- A date, or time, or datetime value is converted to a string.
JSON_ARRAY() Examples
Here are some examples of JSON_ARRAY().
Basic usage
SELECT JSON_ARRAY(123, 'abc', NULL, TRUE, FALSE, NOW());
+---------------------------------------------------------------+
| JSON_ARRAY(123, 'abc', NULL, TRUE, FALSE, NOW()) |
+---------------------------------------------------------------+
| [123, "abc", null, true, false, "2022-04-18 07:47:23.000000"] |
+---------------------------------------------------------------+JSON array
You can create a array which elements are JSON arrays.
SELECT JSON_ARRAY(JSON_ARRAY(123, 456), JSON_ARRAY('abc', 'dec'));
+------------------------------------------------------------+
| JSON_ARRAY(JSON_ARRAY(123, 456), JSON_ARRAY('abc', 'dec')) |
+------------------------------------------------------------+
| [[123, 456], ["abc", "dec"]] |
+------------------------------------------------------------+Here, we use the result of JSON_ARRAY() as a parameter.
JSON object
You can create a array which elements are JSON objects.
SELECT JSON_ARRAY(
JSON_OBJECT('name', 'Jim', 'age', 20),
JSON_OBJECT('name', 'Tim', 'age', 18)
) AS objct_array;
+----------------------------------------------------------+
| objct_array |
+----------------------------------------------------------+
| [{"age": 20, "name": "Jim"}, {"age": 18, "name": "Tim"}] |
+----------------------------------------------------------+Here, we use the result of JSON_OBJECT() as the parameters of JSON_ARRAY().