JSON_APPEND
Description
This function appends a specified value to the end of a specified array in a JSON document and returns the result. It is an alias of the JSON_ARRAY_APPEND() function.
Syntax
JSON_APPEND(json_doc, path, val[, path, val] ...)
Remarks
The json_doc parameter specifies the JSON document, and path specifies the path. If any parameter is NULL, the function returns NULL.
The following situations will result in an error:
-
The
json_docparameter is not a valid JSON document. -
The
pathparameter is not a valid path expression. -
The
pathparameter contains the * or ** wildcard.
The path-value pairs are evaluated from left to right. The document generated by evaluating the path-value pair is used as the new value for the next pair.
If the value corresponding to the path is a scalar or an object, it is automatically wrapped in an array, and the new value is added to the array. Path-value pairs that do not identify an array in the JSON document are ignored.
Examples
-
The following example defines a user variable
@jnthat contains a JSON string representing a JSON array.SET @jn = '["a", ["b", "c"], "d","e"]';Append the value
1to the end of the array at the path$[1]in the@jnvariable.SELECT JSON_APPEND(@jn, '$[1]', 1);Here,
$[1]corresponds to the second element in the JSON array. The result is as follows:+--------------------------------+
| JSON_APPEND(@jn, '$[1]', 1) |
+--------------------------------+
| ["a", ["b", "c", 1], "d", "e"] |
+--------------------------------+
1 row in set (0.001 sec)Append the value
2to the end of the array at the path$[0]in the@jnvariable.SELECT JSON_APPEND(@jn, '$[0]', 2);Here,
$[0]corresponds to the first element in the JSON array. The result is as follows:+----------------------------------+
| JSON_APPEND(@jn, '$[0]', 2) |
+----------------------------------+
| [["a", 2], ["b", "c"], "d", "e"] |
+----------------------------------+
1 row in set (0.001 sec)Since
["b", "c"]is an array, we can append the value3to the end of the array at the path$[1][0]in the@jnvariable.SELECT JSON_APPEND(@jn, '$[1][0]', 3);The result is as follows:
+----------------------------------+
| JSON_APPEND(@jn, '$[1][0]', 3) |
+----------------------------------+
| ["a", [["b", 3], "c"], "d", "e"] |
+----------------------------------+
1 row in set (0.001 sec) -
The following example defines a user variable
@jempthat contains a JSON string representing an object. The object has three key-value pairs: the value of"a"is the number1; the value of"b"is an array containing the numbers2and3; and the value of"c"is the number4.SET @jemp = '{"a": 1, "b": [2, 3], "c": 4}';Append the string
xto the end of the array at the path$.bin the@jempvariable.SELECT JSON_APPEND(@jemp, '$.b', 'x');Here,
$.bcorresponds to the array associated with the"b"key. The result is as follows:+------------------------------------+
| JSON_APPEND(@jemp, '$.b', 'x') |
+------------------------------------+
| {"a": 1, "b": [2, 3, "x"], "c": 4} |
+------------------------------------+
1 row in set (0.001 sec)Similarly, append the string
yto the end of the array at the path$.cin the@jempvariable.SELECT JSON_APPEND(@jemp, '$.c', 'y');Here,
$.ccorresponds to the array associated with the"c"key. The result is as follows:+--------------------------------------+
| JSON_APPEND(@jemp, '$.c', 'y') |
+--------------------------------------+
| {"a": 1, "b": [2, 3], "c": [4, "y"]} |
+--------------------------------------+
1 row in set (0.001 sec) -
The following example defines a user variable
@tempthat contains a simple JSON object. In this object, the key is"a"and the value is the number5.SET @temp = '{"a": 5}';Append the string
zto the end of the array at the path$in the@tempvariable.SELECT JSON_APPEND(@temp, '$', 'z');Note that the path
$refers to the root level of the JSON document, which is the entire JSON object, not a key-value pair within the object. The result is as follows:+------------------------------+
| JSON_APPEND(@temp, '$', 'z') |
+------------------------------+
| [{"a": 5}, "z"] |
+------------------------------+
1 row in set (0.001 sec)