Skip to main content
Version: V1.0.0

JSON_INSERT

Description

This function inserts data into a JSON document and returns the result.

Syntax

JSON_INSERT(json_doc, path, val[, path, val] ...)

Instructions

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_doc parameter is not a valid JSON document.

  • The path parameter is not a valid path expression.

  • The path parameter contains the * or ** wildcard.

Path-value pairs are evaluated from left to right. The document generated by evaluating the path-value pairs is used as the new value for the next pair.

Existing path-value pairs in the document are ignored and do not overwrite existing document values. If the path identifies one of the following types of values, the path-value pairs that do not exist in the document will add the value to the document:

  • A member that does not exist in the existing object. This member is added to the object and associated with the new value.

  • A position beyond the end of the existing array. In this case, 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, path-value pairs that do not exist in the document are ignored and considered invalid.

Examples

SET @jn = '{ "a": 1, "b": [2, 3]}';
Query OK, 0 rows affected (0.000 sec)

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 (0.001 sec)