{"id":21,"date":"2025-10-19T19:59:58","date_gmt":"2025-10-19T17:59:58","guid":{"rendered":"https:\/\/extendsclass.com\/blog\/?p=21"},"modified":"2023-04-29T11:32:45","modified_gmt":"2023-04-29T09:32:45","slug":"introduction-to-json-merge-patch","status":"publish","type":"post","link":"https:\/\/extendsclass.com\/blog\/introduction-to-json-merge-patch","title":{"rendered":"Introduction to JSON Merge Patch"},"content":{"rendered":"\n<p style=\"font-size:68px\"><strong>{JSON Merge Patch}<\/strong><\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_47_1 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"ez-toc-toggle-icon-1\"><label for=\"item-69e0141e3d3a1\" aria-label=\"Table of Content\"><span style=\"display: flex;align-items: center;width: 35px;height: 30px;justify-content: center;direction:ltr;\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/label><input  type=\"checkbox\" id=\"item-69e0141e3d3a1\"><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/extendsclass.com\/blog\/introduction-to-json-merge-patch\/#What_is_it_What_is_it_used_for\" title=\"What is it? What is it used for?\">What is it? What is it used for?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/extendsclass.com\/blog\/introduction-to-json-merge-patch\/#The_interest_of_JSON_Merge_Patch\" title=\"The interest of JSON Merge Patch\">The interest of JSON Merge Patch<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/extendsclass.com\/blog\/introduction-to-json-merge-patch\/#And_concretely\" title=\"And concretely?\">And concretely?<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/extendsclass.com\/blog\/introduction-to-json-merge-patch\/#Modifying_an_attribute\" title=\"Modifying an attribute\">Modifying an attribute<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/extendsclass.com\/blog\/introduction-to-json-merge-patch\/#Add_an_attribute\" title=\"Add an attribute\">Add an attribute<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/extendsclass.com\/blog\/introduction-to-json-merge-patch\/#To_delete_an_attribute\" title=\"To delete an attribute\">To delete an attribute<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/extendsclass.com\/blog\/introduction-to-json-merge-patch\/#REST_API\" title=\"REST API\">REST API<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/extendsclass.com\/blog\/introduction-to-json-merge-patch\/#Limitations\" title=\"Limitations\">Limitations<\/a><ul class='ez-toc-list-level-4'><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/extendsclass.com\/blog\/introduction-to-json-merge-patch\/#%E2%80%9CNull_value%E2%80%9D\" title=\"&#8220;Null value&#8221;\">&#8220;Null value&#8221;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/extendsclass.com\/blog\/introduction-to-json-merge-patch\/#Updating_an_Array\" title=\"Updating an Array\">Updating an Array<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/extendsclass.com\/blog\/introduction-to-json-merge-patch\/#PHP_code_snippet_to_handle_JSON_Merge_Patch\" title=\"PHP code snippet to handle JSON Merge Patch\">PHP code snippet to handle JSON Merge Patch<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/extendsclass.com\/blog\/introduction-to-json-merge-patch\/#JSON_Merge_Path_vs_JSON_Patch\" title=\"JSON Merge Path vs JSON Patch\">JSON Merge Path vs JSON Patch<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/extendsclass.com\/blog\/introduction-to-json-merge-patch\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_it_What_is_it_used_for\"><\/span>What is it? What is it used for?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>JSON Merge Patch is a data format that allows describing modifications to be made to a JSON document.<\/p>\n\n\n\n<p>This format can be used in REST APIs to handle partial modifications of resources via the HTTP PATCH method.<\/p>\n\n\n\n<p>The JSON Merge Patch format is itself a JSON document! In short, this JSON only contains the modifications to be made.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_interest_of_JSON_Merge_Patch\"><\/span>The interest of JSON Merge Patch<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>When updating a resource in a REST API, we typically use the PUT method.<\/p>\n\n\n\n<p>The PUT method updates the entire resource, which can pose several issues:<\/p>\n\n\n\n<ul>\n<li>We need to have the entire resource to update it.<\/li>\n\n\n\n<li>When the resource is large, updating it can quickly become resource-intensive.<\/li>\n<\/ul>\n\n\n\n<p>This is where the PATCH method and the use of JSON Merge Patch come in. They provide the ability to partially update a resource!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"And_concretely\"><\/span>And concretely?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Modifying_an_attribute\"><\/span>Modifying an attribute<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Let&#8217;s take the example of the resource \/people\/cyril<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n\t\"firstname\": \"Cyril\",\n\t\"job\": \"Developer\",\n\t\"hobbies\": &#91;\"cooking\"]\n}<\/code><\/pre>\n\n\n\n<p>If we want to change the job of this person, with the PUT method, we need to completely send back the resource:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n\t\"firstname\": \"Cyril\",\n\t\"job\": \"\nProject Manager\",\n\t\"hobbies\": &#91;\"Cooking\"]\n}<\/code><\/pre>\n\n\n\n<p>Now with JSON Merge Patch, we only send what needs to be updated:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n\t\"job\": \"Project Manager\"\n}<\/code><\/pre>\n\n\n\n<p>And voila, this format is quite simple and intuitive!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Add_an_attribute\"><\/span>Add an attribute<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Now, if we want to add an attribute, we just need to add it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n\t\"birthday\": \"1982-10-14\"\n}<\/code><\/pre>\n\n\n\n<p>We get the following result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n\t\"firstname\": \"Cyril\",\n\t\"job\": \"\nProject Manager\",\n\t\"hobbies\": &#91;\"Cooking\"],\n\t\"birthday\": \"1982-10-14\"\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"To_delete_an_attribute\"><\/span>To delete an attribute<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>To delete an attribute, simply indicate it with a null value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n\t\"birthday\": null\n}<\/code><\/pre>\n\n\n\n<p>The resulting JSON would be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n\t\"firstname\": \"Cyril\",\n\t\"job\": \"\nProject Manager\",\n\t\"hobbies\": &#91;\"Cooking\"]\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"REST_API\"><\/span>REST API<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Ce format est donc \u00e0 utiliser avec la m\u00e9thode HTTP PATCH.<\/p>\n\n\n\n<p>Son media type est:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>application\/json+merge-patch<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Limitations\"><\/span>Limitations<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>JSON Merge Patch is really simple to use, and its learning curve is quick. Unfortunately, this format has several limitations, some of which are quite inconvenient.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E2%80%9CNull_value%E2%80%9D\"><\/span>&#8220;Null value&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>As we saw in the &#8220;Removing an Attribute&#8221; section, the null value is used to remove a node. Therefore, it is impossible to assign the null value!<\/p>\n\n\n\n<p>Functionally, this point may not be very problematic.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Updating_an_Array\"><\/span>Updating an Array<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>From my point of view, this is the most annoying point with this format. Indeed, arrays cannot be partially updated!<\/p>\n\n\n\n<p>If I want to add a hobby, I have to completely send the array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n\t\"hobbies\": &#91;\"Cooking\", \"Running\"]\n}<\/code><\/pre>\n\n\n\n<p>The JSON Merge Patch only allows partial updates for objects. Everything that is not an object will be completely replaced.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"PHP_code_snippet_to_handle_JSON_Merge_Patch\"><\/span>PHP code snippet to handle JSON Merge Patch<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Managing partial updates via JSON Merge Patch is quite simple in PHP. Here is an example code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function JSONMergePatch($target, $patch) {\n\tif (is_object($patch)) {\n\t\tif (!is_object($target)) {\n\t\t\t$target = new \\stdclass();\n\t\t}\n\t\tforeach($patch as $name =&gt; $value) {\n\t\t\tif ($value === null) {\n\t\t\t\t\/\/ When receiving a null value, we delete the corresponding node.\n\t\t\t\tif (property_exists($target, $name)) {\n\t\t\t\t\tunset($target-&gt;$name);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif (!isset($target-&gt;$name)) {\n\t\t\t\t\t$target-&gt;$name = null;\n\t\t\t\t}\n\t\t\t\t\/\/ Recursive call to handle sub-trees\n\t\t\t\t$target-&gt;$name = JSONMergePatch($target-&gt;$name, $value);\n\t\t\t}\n\t\t}\n\t\treturn $target;\n\t} else {\n\t\t\/\/ This is not an object, we replace the node\n\t\treturn $patch;\n\t}\n}\t<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"JSON_Merge_Path_vs_JSON_Patch\"><\/span>JSON Merge Path vs JSON Patch<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>JSON Merge Patch is not the only format available to handle partial updates in APIs.<\/p>\n\n\n\n<p>One of its competitors is JSON Patch, which is less intuitive but has the advantage of not having the limitations of JSON Merge Patch.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To summarize, JSON Merge Patch is intuitive and easy to implement. However, it is quite limited and loses its relevance when managing arrays.<\/p>\n\n\n\n<p>For more information, see the <a href=\"https:\/\/tools.ietf.org\/html\/rfc7396\" title=\"\">RFC<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSON Merge Patch is a data format that allows describing modifications to be made to a JSON document.<\/p>\n","protected":false},"author":1,"featured_media":51,"comment_status":"open","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"_sitemap_exclude":false,"_sitemap_priority":"","_sitemap_frequency":""},"categories":[2],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts\/21"}],"collection":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/comments?post=21"}],"version-history":[{"count":11,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts\/21\/revisions"}],"predecessor-version":[{"id":40,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts\/21\/revisions\/40"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/media\/51"}],"wp:attachment":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/media?parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/categories?post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/tags?post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}