Purpose
You can call this function to insert data into a JSON document and return the result.
Syntax
JSON_INSERT(json_doc, path, val[, path, val] ...)
Notes
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.
The 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.
A path-value pair for an existing path in the document is ignored. It does not overwrite the existing document value. A path-value pair for a nonexisting path in the document adds the value to the document if the path identifies one of the following types of values:
A member that does not exist in an existing object. The member is added to the object and associated with the new value.
A position that falls beyond the end of an existing array. In this case, the array is extended by using the new value. If the existing value is not an array, it is automatically wrapped into an array and then extended by using the new value.
Otherwise, a path-value pair for a nonexisting path in the document has no effect and is ignored.
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