JSON_ MERGE_ PATCH
On this page
A built-in function that takes two JSON objects and merges them into a single JSON object.
Syntax
JSON_MERGE_PATCH(<json_1>, <json_2>)
Arguments
-
<json_
,1 <json_
: Valid JSON objects or arrays, or expressions that evaluate to valid JSON objects or arrays.2
Remarks
-
If either of the two arguments is not a JSON object, the result of the merge is the second argument.
-
If both arguments are JSON objects, then:
-
If both arguments contain the same key and the value of this key in the second argument is not
NULL
, the result includes the value from the second argument.Otherwise, the key is excluded from the result. -
If a key is present in only one argument, its key-value pair is included in the result.
-
-
If both arguments are not JSON objects, the result is
NULL
. -
The
JSON_
function takes two arguments at a time and can be embedded to work iteratively on multiple arguments.MERGE_ PATCH It merges the arguments, starting from the innermost function call until all the json arguments are merged. -
The
JSON_
function works recursively.MERGE_ PATCH When both arguments contain pairs with the same keys that are nested JSON objects, the function merges these nested JSON objects for that key.
Examples
The following table provides several examples of JSON_
.
Query |
Result |
Comments |
---|---|---|
|
|
Both arguments are JSON objects with unique keys, therefore both are included in the result. |
|
|
The key |
|
|
The first argument is not a JSON object, therefore the result is the second argument. |
|
|
The second argument is not a JSON object, therefore the result is the second argument. |
|
|
The value of the second argument is |
|
|
Arrays are not merged, therefore the result is the second argument. |
|
|
The second argument is an empty object and the first argument is a JSON object, therefore the result of the merge is the first argument. |
|
|
The function works recursively. |
|
|
The first argument is not a valid JSON object or array, therefore the result is |
|
|
The key |
|
|
The function takes only two arguments, therefore passing the third argument gives an |
|
|
The function works iteratively with three arguments. |
Example Data
The examples in this section use the following table and values:
CREATE TABLE json_docs (id INT, json_data JSON);INSERT INTO json_docs VALUES(1, '{"firstname":"Andrew"}'),(2, '{"firstname":"Julius","lastname":"Caesar"}'),(3, '{"firstname":"Angus","lastname":"Young"}'),(4, '{"firstname":"Randall","lastname":"McMurphy"}'),(5, '["Mango","Apple","Orange"]'),(6, '{"Fruits":["Mango","Apple","Orange"]}'),(7, '{"firstname":"Edna","lastname":"Mode"}'),(8, '{"name":{"firstname":"Norman"}}'),(9, '{"name":{"firstname":"John"}}'),(10, '{"firstname": "Simon"}');
SELECT * FROM json_docsORDER BY id;
+------+-----------------------------------------------+
| id | json_data |
+------+-----------------------------------------------+
| 1 | {"firstname":"Andrew"} |
| 2 | {"firstname":"Julius","lastname":"Caesar"} |
| 3 | {"firstname":"Angus","lastname":"Young"} |
| 4 | {"firstname":"Randall","lastname":"McMurphy"} |
| 5 | ["Mango","Apple","Orange"] |
| 6 | {"Fruits":["Mango","Apple","Orange"]} |
| 7 | {"firstname":"Edna","lastname":"Mode"} |
| 8 | {"name":{"firstname":"Norman"}} |
| 9 | {"name":{"firstname":"John"}} |
| 10 | {"firstname":"Simon"} |
+------+-----------------------------------------------+
Example 1
Returns the merged result, since both arguments are JSON objects with unique keys.
SELECT JSON_MERGE_PATCH((SELECT json_data FROM json_docs WHERE id = 1),'{"lastname":"Cloud"}')AS Result;
+-------------------------------------------+
| Result |
+-------------------------------------------+
| {"firstname":"Andrew","lastname":"Cloud"} |
+-------------------------------------------+
Example 2
Returns the value from the second argument, "Augustus”
, since the key “firstname”
is present in both JSON objects.
SELECT JSON_MERGE_PATCH((SELECT json_data FROM json_docs WHERE id = 2),'{"firstname":"Augustus"}')AS Result;
+----------------------------------------------+
| Result |
+----------------------------------------------+
| {"firstname":"Augustus","lastname":"Caesar"} |
+----------------------------------------------+
Example 3
Returns the second argument, since the first argument is not a JSON object.
SELECT JSON_MERGE_PATCH((SELECT json_data FROM json_docs WHERE id = 5),'{"Fruits":"Banana"}')AS Result;
+---------------------+
| Result |
+---------------------+
| {"Fruits":"Banana"} |
+---------------------+
Example 4
Returns the second argument, since arrays are not merged.
SELECT JSON_MERGE_PATCH((SELECT json_data FROM json_docs WHERE id = 6),'{"Fruits":["Banana","Pears","Grapes"]}')AS Result;
+----------------------------------------+
| Result |
+----------------------------------------+
| {"Fruits":["Banana","Pears","Grapes"]} |
+----------------------------------------+
Example 5
The function works recursively."name"
are both JSON objects, therefore the objects {"firstname":"Norman"}
and {"lastname":"Lewis"}
are merged to create the result value.
SELECT JSON_MERGE_PATCH((SELECT json_data FROM json_docs WHERE id = 8),'{"name":{"lastname":"Lewis"}}')AS Result;
+----------------------------------------------------+
| Result |
+----------------------------------------------------+
| {"name":{"firstname":"Norman","lastname":"Lewis"}} |
+----------------------------------------------------+
Example 6
Returns the second argument, since the key “name”
is present in both the JSON objects and merging of the JSON objects {"firstname":"John"}
and {"firstname":"Tom"}
results in the second argument.
SELECT JSON_MERGE_PATCH((SELECT json_data FROM json_docs WHERE id = 9),'{"name":{"firstname":"Tom"}}')AS Result;
+------------------------------+
| Result |
+------------------------------+
| {"name":{"firstname":"Tom"}} |
+------------------------------+
Example 7
The function works iteratively with three arguments.
SELECT JSON_MERGE_PATCH(JSON_MERGE_PATCH((SELECT json_data FROM json_docs WHERE id = 10),'{"lastname":"Grover"}'),'{"profession":"Actor"}')AS Result;
+----------------------------------------------------------------+
| Result |
+----------------------------------------------------------------+
| {"firstname":"Simon","lastname":"Grover","profession":"Actor"} |
+----------------------------------------------------------------+
Example 8
Returns the merged result for all the selected columns.
SELECT JSON_MERGE_PATCH(json_data,'{"profession":"author"}') AS ResultFROM json_docs ORDER BY id LIMIT 4;
+---------------------------------------------------------------------+
| Result |
+---------------------------------------------------------------------+
| {"firstname":"Andrew","profession":"author"} |
| {"firstname":"Julius","lastname":"Caesar","profession":"author"} |
| {"firstname":"Angus","lastname":"Young","profession":"author"} |
| {"firstname":"Randall","lastname":"McMurphy","profession":"author"} |
+---------------------------------------------------------------------+
Example 9
The function can take two variables once they are set.
SET @x = '{"firstname":"Ben","lastname":"Parker"}',@y = '{"firstname":"Peter","profession":"Spiderman"}';SELECT JSON_MERGE_PATCH(@x,@y) AS MarvelCharacter;
+--------------------------------------------------------------------+
| MarvelCharacter |
+--------------------------------------------------------------------+
| {"firstname":"Peter","lastname":"Parker","profession":"Spiderman"} |
+--------------------------------------------------------------------+
Last modified: November 12, 2024