JSON_MERGE_PRESERVE()¶
JSON_MERGE_PRESERVE(doc…) merges two or more JSON documents by preserving the values associated with duplicate keys under one key and combining array elements, unlike JSON_MERGE_PATCH() which overwrites and deletes members.
Description¶
JSON_MERGE_PRESERVE(json [, json] ...) merges the input documents from left to right while preserving all values:
Object values with the same key are merged recursively; other conflicting values are combined into an array.
Arrays are concatenated.
Scalar values that cannot be merged are combined into an array.
This behavior preserves values while recursively merging compatible objects. Use JSON_MERGE_PATCH() when you want RFC 7396 overwrite-and-delete semantics instead.
Syntax¶
JSON_MERGE_PRESERVE(json [, json] ...)
Arguments¶
Argument |
Description |
|---|---|
|
Two or more JSON documents to merge. |
Return Value¶
Returns the merged JSON document.
Examples¶
DROP DATABASE IF EXISTS json_merge_preserve_demo;
CREATE DATABASE json_merge_preserve_demo;
USE json_merge_preserve_demo;
SELECT JSON_MERGE_PRESERVE('{"a":1,"b":2}', '{"a":3,"c":4}') AS merged;
SELECT JSON_MERGE_PRESERVE('[1,2]', '[true,false]') AS array_merge;
SELECT JSON_MERGE_PRESERVE('{"a":{"x":1}}', '{"a":{"y":2}}') AS nested_merge;
DROP DATABASE json_merge_preserve_demo;