MySQL tutorial: JSON_ARRAY_APPEND [EN]
top of page
CerebroSQL

MySQL: 

JSON_ARRAY_APPEND

Syntax:
JSON_ARRAY_APPEND(json_doc, path, val[, path, val] ...)

Appends values to the end of the indicated arrays within a JSON
document and returns the result. Returns NULL if any argument is NULL.
An error occurs if the json_doc argument is not a valid JSON document
or any path argument is not a valid path expression or contains a * or
** wildcard.

The path-value pairs are evaluated left to right. The document produced
by evaluating one pair becomes the new value against which the next
pair is evaluated.

If a path selects a scalar or object value, that value is autowrapped
within an array and the new value is added to that array. Pairs for
which the path does not identify any value in the JSON document are
ignored.

URL: https://dev.mysql.com/doc/refman/8.0/en/json-modification-functions.html

Example

mysql> SET @j = '["a", ["b", "c"], "d"]';
mysql> SELECT JSON_ARRAY_APPEND(@j, '$[1]', 1);
+----------------------------------+
| JSON_ARRAY_APPEND(@j, '$[1]', 1) |
+----------------------------------+
| ["a", ["b", "c", 1], "d"] |
+----------------------------------+
mysql> SELECT JSON_ARRAY_APPEND(@j, '$[0]', 2);
+----------------------------------+
| JSON_ARRAY_APPEND(@j, '$[0]', 2) |
+----------------------------------+
| [["a", 2], ["b", "c"], "d"] |
+----------------------------------+
mysql> SELECT JSON_ARRAY_APPEND(@j, '$[1][0]', 3);
+-------------------------------------+
| JSON_ARRAY_APPEND(@j, '$[1][0]', 3) |
+-------------------------------------+
| ["a", [["b", 3], "c"], "d"] |
+-------------------------------------+

mysql> SET @j = '{"a": 1, "b": [2, 3], "c": 4}';
mysql> SELECT JSON_ARRAY_APPEND(@j, '$.b', 'x');
+------------------------------------+
| JSON_ARRAY_APPEND(@j, '$.b', 'x') |
+------------------------------------+
| {"a": 1, "b": [2, 3, "x"], "c": 4} |
+------------------------------------+
mysql> SELECT JSON_ARRAY_APPEND(@j, '$.c', 'y');
+--------------------------------------+
| JSON_ARRAY_APPEND(@j, '$.c', 'y') |
+--------------------------------------+
| {"a": 1, "b": [2, 3], "c": [4, "y"]} |
+--------------------------------------+

mysql> SET @j = '{"a": 1}';
mysql> SELECT JSON_ARRAY_APPEND(@j, '$', 'z');
+---------------------------------+
| JSON_ARRAY_APPEND(@j, '$', 'z') |
+---------------------------------+
| [{"a": 1}, "z"] |
+---------------------------------+

bottom of page