Purpose
This function inserts data into a JSON document and returns the result.
Syntax
JSON_INSERT(json_doc, path, val[, path, val] ...)
Considerations
The json_doc parameter specifies the JSON document, and path is the path parameter. 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.
Path-value pairs are evaluated from left to right. The document generated by the path-value pair is used as the new value for the next pair.
Existing path-value pairs in the document will be ignored and will not overwrite existing document values. If the path identifies one of the following types of values, the path-value pair will be added to the document:
A member that does not exist in the existing object. The member is added to the object and associated with the new value.
A position beyond the end of the existing array. The array is extended with the new value. If the existing value is not an array, it is automatically wrapped in an array and then extended with the new value.
Otherwise, the path-value pair will be ignored and considered invalid.
Examples
obclient> SET @jn = '{ "a": 1, "b": [2, 3]}';
Query OK, 0 rows affected
obclient> SELECT JSON_INSERT(@jn, '$.a', 10, '$.c', '[true, false]');
+-----------------------------------------------------+
| JSON_INSERT(@jn, '$.a', 10, '$.c', '[true, false]') |
+-----------------------------------------------------+
| {"a": 1, "b": [2, 3], "c": "[true, false]"} |
+-----------------------------------------------------+
1 row in set
