In MariaDB, JSON_MERGE_PATCH() is een ingebouwde functie die twee of meer JSON-documenten samenvoegt en het resultaat retourneert.
De JSON_MERGE_PATCH() functie is een RFC 7396-compatibele vervanging voor de JSON_MERGE() functie, die is verouderd.
Syntaxis
De syntaxis gaat als volgt:
JSON_MERGE_PATCH(json_doc, json_doc[, json_doc] ...)
Waar json_doc zijn de JSON-documenten die moeten worden samengevoegd.
Voorbeeld
Hier is een voorbeeld om te demonstreren.
SELECT JSON_MERGE_PATCH('{"name":"Wag"}', '{"type":"Dog"}'); Resultaat:
+------------------------------------------------------+
| JSON_MERGE_PATCH('{"name":"Wag"}', '{"type":"Dog"}') |
+------------------------------------------------------+
| {"name": "Wag", "type": "Dog"} |
+------------------------------------------------------+ We kunnen zien dat de twee documenten zijn samengevoegd tot één.
Hier is een voorbeeld dat drie documenten samenvoegt:
SELECT JSON_MERGE_PATCH(
'{ "name" : "Wag" }',
'{ "type" : "Dog" }',
'{ "score" : [ 9, 7, 8 ] }'
) AS Result; Resultaat:
+----------------------------------------------------+
| Result |
+----------------------------------------------------+
| {"name": "Wag", "type": "Dog", "score": [9, 7, 8]} |
+----------------------------------------------------+ Arrays
De JSON_MERGE_PATCH() functie voegt geen arrays samen:
SELECT JSON_MERGE_PATCH(
'[1,2,3]',
'[4,5,6]'
) AS Result; Resultaat:
+-----------+ | Result | +-----------+ | [4, 5, 6] | +-----------+
Dit in tegenstelling tot de (verouderde) JSON_MERGE() functie en zijn synoniem JSON_MERGE_PRESERVE() , die beide arrays samenvoegen.
Formaat van het resultaat
Het is je misschien opgevallen dat JSON_MERGE_PATCH() voegt spaties toe aan het resulterende document. Als dit een probleem is, kunt u JSON_COMPACT() . gebruiken om de spatie te verwijderen.
Voorbeeld:
SELECT
JSON_COMPACT(
JSON_MERGE_PATCH(
'{"name":"Wag"}',
'{"type":"Dog"}',
'{"score":[9,7,8]}'
)
) AS Result; Resultaat:
+---------------------------------------------+
| Result |
+---------------------------------------------+
| {"name":"Wag","type":"Dog","score":[9,7,8]} |
+---------------------------------------------+
Maar als je de andere kant op moet en extra opmaak wilt krijgen, zoals ingesprongen structuren, probeer dan de JSON_DETAILED() functie.
SELECT
JSON_DETAILED(
JSON_MERGE_PATCH(
'{ "name" : "Wag" }',
'{ "type" : "Dog" }',
'{ "score" : [ 9, 7, 8 ] }'
)
) AS Result; Resultaat:
+---------------------------------------+
| Result |
+---------------------------------------+
| {
"name": "Wag",
"type": "Dog",
"score":
[
9,
7,
8
]
} |
+---------------------------------------+ Nullargument
Als een argument NULL is , het resultaat is NULL :
SELECT
JSON_MERGE_PATCH('{"a":1}', null) AS a,
JSON_MERGE_PATCH(null, '{"a":1}') AS b,
JSON_MERGE_PATCH(null, null) AS c; Resultaat:
+------+------+------+ | a | b | c | +------+------+------+ | NULL | NULL | NULL | +------+------+------+
Onjuist aantal parameters
Het aanroepen van de functie zonder argumenten resulteert in een fout:
SELECT JSON_MERGE_PATCH(); Resultaat:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_MERGE_PATCH'
Het is hetzelfde als u slechts één argument geeft:
SELECT JSON_MERGE_PATCH('{"a":1}'); Resultaat:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_MERGE_PATCH'