Skip to main content
Version: V1.0.0

JSON_MERGE_PRESERVE

Description

Merges two or more JSON documents and returns the merged result.

Syntax

JSON_MERGE_PRESERVE(json_doc, json_doc[, json_doc] ...)

Description

The json_doc parameter specifies the JSON documents to be merged.

If any parameter is NULL, the function returns NULL. If any parameter is not a valid JSON document, an error is returned.

The JSON_MERGE_PRESERVE() function merges documents according to the following rules:

  • Adjacent arrays are merged into one array.

  • Adjacent objects are merged into one object.

  • Scalar values are automatically wrapped in arrays and merged into arrays.

  • Adjacent arrays and objects are merged by wrapping objects in arrays.

Examples

SELECT JSON_MERGE_PRESERVE('[1, 2, 3]', '[true, false]');
+---------------------------------------------------+
| JSON_MERGE_PRESERVE('[1, 2, 3]', '[true, false]') |
+---------------------------------------------------+
| [1, 2, 3, true, false] |
+---------------------------------------------------+
1 row in set (0.001 sec)

SELECT JSON_MERGE_PRESERVE('{"name": "apple"}', '{"id": 56}');
+--------------------------------------------------------+
| JSON_MERGE_PRESERVE('{"name": "apple"}', '{"id": 56}') |
+--------------------------------------------------------+
| {"id": 56, "name": "apple"} |
+--------------------------------------------------------+
1 row in set (0.001 sec)

SELECT JSON_MERGE_PRESERVE('1', 'true');
+----------------------------------+
| JSON_MERGE_PRESERVE('1', 'true') |
+----------------------------------+
| [1, true] |
+----------------------------------+
1 row in set (0.001 sec)

SELECT JSON_MERGE_PRESERVE('[1, 2, 3]', '{"id": 56}');
+------------------------------------------------+
| JSON_MERGE_PRESERVE('[1, 2, 3]', '{"id": 56}') |
+------------------------------------------------+
| [1, 2, 3, {"id": 56}] |
+------------------------------------------------+
1 row in set (0.001 sec)

SELECT JSON_MERGE_PRESERVE('{ "a": 1, "b": 2, "c": 5}', '{ "a": 3, "c": 4 }');
+-------------------------------------------------------------------------+
| JSON_MERGE_PRESERVE('{ "a": 1, "b": 2, "c": 5}', '{ "a": 3, "c": 4 }') |
+-------------------------------------------------------------------------+
| {"a": [1, 3], "b": 2, "c": [5, 4]} |
+-------------------------------------------------------------------------+
1 row in set (0.001 sec)

SELECT JSON_MERGE_PRESERVE('{ "a": 1, "b": 2 }','{ "a": 3, "c": 4 }', '{ "a": 5, "d": 6 }');
+--------------------------------------------------------------------------------------+
| JSON_MERGE_PRESERVE('{ "a": 1, "b": 2 }','{ "a": 3, "c": 4 }', '{ "a": 5, "d": 6 }') |
+--------------------------------------------------------------------------------------+
| {"a": [1, 3, 5], "b": 2, "c": 4, "d": 6} |
+--------------------------------------------------------------------------------------+
1 row in set (0.001 sec)