Purpose
JSON_ARRAY_APPEND() appends the specified values to the end of the specified arrays in a JSON document and returns the result. The function is an alias for JSON_APPEND().
Syntax
JSON_ARRAY_APPEND(json_doc, path, val[, path, val] ...)
Description
json_doc specifies the name of the JSON document, and path specifies the path. If any argument is NULL, the return value is NULL.
An error occurs in the following cases:
The JSON document specified for
json_docis invalid.The path expression specified for
pathis invalid.The specified
pathcontains a * or ** wildcard.
Path-value pairs are evaluated from left to right. The document generated by using an evaluated path-value pair is used as a new value to evaluate the next pair.
If the value corresponding to the path is a scalar or object value, the value is automatically wrapped in an array, and the new value is added to the array. The path-value pairs for which the path does not identify any array in the JSON document are ignored.
Examples
obclient> SET @jn = '["a", ["b", "c"], "d","e"]';
Query OK, 0 rows affected
obclient> SELECT JSON_ARRAY_APPEND(@jn, '$[1]', 1);
+----------------------------------+
| JSON_ARRAY_APPEND(@jn, '$[1]', 1) |
+----------------------------------+
| ["a", ["b", "c", 1], "d","e"] |
+----------------------------------+
1 row in set
obclient> SELECT JSON_ARRAY_APPEND(@jn, '$[0]', 2);
+----------------------------------+
| JSON_ARRAY_APPEND(@jn, '$[0]', 2) |
+----------------------------------+
| [["a", 2], ["b", "c"], "d","e"] |
+----------------------------------+
1 row in set
obclient> SELECT JSON_ARRAY_APPEND(@jn, '$[1][0]', 3);
+-------------------------------------+
| JSON_ARRAY_APPEND(@jn, '$[1][0]', 3) |
+-------------------------------------+
| ["a", [["b", 3], "c"], "d","e"] |
+-------------------------------------+
1 row in set
obclient> SET @jemp = '{"a": 1, "b": [2, 3], "c": 4}';
Query OK, 0 rows affected
obclient> SELECT JSON_ARRAY_APPEND(@jn, '$.b', 'x');
+------------------------------------+
| JSON_ARRAY_APPEND(@jn, '$.b', 'x') |
+------------------------------------+
| {"a": 1, "b": [2, 3, "x"], "c": 4} |
+------------------------------------+
1 row in set
obclient> SELECT JSON_ARRAY_APPEND(@jn, '$.c', 'y');
+--------------------------------------+
| JSON_ARRAY_APPEND(@jn, '$.c', 'y') |
+--------------------------------------+
| {"a": 1, "b": [2, 3], "c": [4, "y"]} |
+--------------------------------------+
1 row in set
obclient> SET @jn = '{"a": 5}';
Query OK, 0 rows affected
obclient> SELECT JSON_ARRAY_APPEND(@jn, '$', 'z');
+---------------------------------+
| JSON_ARRAY_APPEND(@jn, '$', 'z') |
+---------------------------------+
| [{"a": 5}, "z"] |
+---------------------------------+
1 row in set