{"id":71,"date":"2023-05-19T17:11:57","date_gmt":"2023-05-19T15:11:57","guid":{"rendered":"https:\/\/extendsclass.com\/blog\/?p=71"},"modified":"2023-05-08T16:36:23","modified_gmt":"2023-05-08T14:36:23","slug":"json-patch","status":"publish","type":"post","link":"https:\/\/extendsclass.com\/blog\/json-patch","title":{"rendered":"JSON Patch"},"content":{"rendered":"\n<p>In a previous article, I presented JSON Merge Patch. <strong>JSON Patch<\/strong> is simply an alternative!<\/p>\n\n\n\n<p>It is a format that allows to manage the partial modification of JSON document. It is used in REST APIs, via the <strong>HTTP PATCH method<\/strong>. Its mime type (Media Type) is <strong>application\/json-patch+json<\/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-69daafb22d9e2\" 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-69daafb22d9e2\"><\/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\/json-patch\/#JSON_Patch_Format\" title=\"JSON Patch Format\">JSON Patch Format<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/extendsclass.com\/blog\/json-patch\/#Les_operations\" title=\"Les op\u00e9rations\">Les op\u00e9rations<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/extendsclass.com\/blog\/json-patch\/#JSON_Pointer\" title=\"JSON Pointer\">JSON Pointer<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/extendsclass.com\/blog\/json-patch\/#Some_examples\" title=\"Some examples\">Some examples<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/extendsclass.com\/blog\/json-patch\/#Add_attribute\" title=\"Add attribute\">Add 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\/json-patch\/#Add_an_element_to_an_array\" title=\"Add an element to an array\">Add an element to an array<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/extendsclass.com\/blog\/json-patch\/#Modify_a_value\" title=\"Modify a value\">Modify a value<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/extendsclass.com\/blog\/json-patch\/#Null_value\" title=\"Null value\">Null value<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/extendsclass.com\/blog\/json-patch\/#Limitations_Disadvantages\" title=\"Limitations \/ Disadvantages\">Limitations \/ Disadvantages<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/extendsclass.com\/blog\/json-patch\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"JSON_Patch_Format\"><\/span>JSON Patch Format<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A patch JSON document is simply a JSON file containing an array of patch operations.<\/p>\n\n\n\n<p>Here is a small example to get an idea of this format:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\n\t{ \"op\": \"add\", \"path\": \"\/hobbies\/1\", \"value\": \"Sport\" },\n\t{ \"op\": \"remove\", \"path\": \"\/job\" }\n]<\/code><\/pre>\n\n\n\n<p>In this example, there are two operations:<\/p>\n\n\n\n<ul>\n<li>An addition of <strong>Sport <\/strong>value.<\/li>\n\n\n\n<li>Deleting the <strong>job <\/strong>value<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Les_operations\"><\/span>Les op\u00e9rations<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>List of operations:<\/p>\n\n\n\n<ul>\n<li>add: Adds an attribute to an object or adds an element to an array. <br>For arrays:\n<ul>\n<li>The element is inserted before the specified index.<\/li>\n\n\n\n<li>The \u201c\u2013\u201d character inserts a value at the end of the array.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>remove: Removes a value.<\/li>\n\n\n\n<li>replace: Replaces a value.<\/li>\n\n\n\n<li>move: Moves a value.<\/li>\n\n\n\n<li>copy: Copies a value.<\/li>\n\n\n\n<li>test: Verifies that a value is well defined. Otherwise the update fails.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"JSON_Pointer\"><\/span>JSON Pointer<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This format can be found in the &#8220;<strong>path<\/strong>&#8221; attribute of the operations.<\/p>\n\n\n\n<p>The JSON pointer (IETF RFC 6901) is used to identify a specific value in a JSON document.<\/p>\n\n\n\n<p>This is similar to JSONPath except that it allows you to search and return multiple values.<\/p>\n\n\n\n<p>This format is quite simple:<\/p>\n\n\n\n<p><br>Take as an example the resource \/persons\/cyril<\/p>\n\n\n\n<ul>\n<li>The path starts with a slash<br><\/li>\n\n\n\n<li>Each segment is separated by a slash<br><\/li>\n\n\n\n<li>Each segment is either the name of an attribute (for objects) or an index (for arrays)<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n\t\"firstname\": \"Cyril\",\n\t\"job\": \"Developer\",\n\t\"hobbies\": &#91;\"cooking\"],\n\t\"pets\" : &#91;\n\t\t{\n\t\t\t\"type\": \"cat\",\n\t\t\t\"name\": \"chanel\"\n\t\t}\n\t]\n}<\/code><\/pre>\n\n\n\n<p>The name attribute has path <strong>\/pets\/0\/name<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Some_examples\"><\/span>Some examples<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Add_attribute\"><\/span>Add attribute<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Let&#8217;s add an <strong>eye<\/strong> attribute to our resource resource \/people\/cyril:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\n\t{ \"op\": \"add\", \"path\": \"\/eye\", \"value\": \"Green\" }\n]<\/code><\/pre>\n\n\n\n<p>The result:<\/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\t\"pets\" : &#91;\n\t\t{\n\t\t\t\"type\": \"cat\",\n\t\t\t\"name\": \"chanel\"\n\t\t}\n\t],\n\t\"eye\": \"Green\",\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Add_an_element_to_an_array\"><\/span>Add an element to an array<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Now let&#8217;s add a <strong>pet<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\n\t{ \"op\": \"add\", \"path\": \"\/pets\/0\", \"value\": {\"type\": \"cat\", \"name\": \"Horu\"} }\n]<\/code><\/pre>\n\n\n\n<p>The result:<\/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\t\"pets\" : &#91;\n\t\t{\n\t\t\t\"type\": \"cat\",\n\t\t\t\"name\": \"Horu\"\n\t\t}\n,\n\t\t{\n\t\t\t\"type\": \"cat\",\n\t\t\t\"name\": \"chanel\"\n\t\t}\n\t],\n\t\"eye\": \"Green\",\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Modify_a_value\"><\/span>Modify a value<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We are going to change the <strong>job<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\n\t{ \"op\": \"add\", \"path\": \"\/job\", \"value\": \"Project Manager\" }\n]<\/code><\/pre>\n\n\n\n<p>The result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n\t\"firstname\": \"Cyril\",\n\t\"job\": \"Project Manager\",\n\t\"hobbies\": &#91;\"cooking\"],\n\t\"pets\" : &#91;\n\t\t{\n\t\t\t\"type\": \"cat\",\n\t\t\t\"name\": \"Horu\"\n\t\t}\n,\n\t\t{\n\t\t\t\"type\": \"cat\",\n\t\t\t\"name\": \"chanel\"\n\t\t}\n\t],\n\t\"eye\": \"Green\",\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Null_value\"><\/span>Null value<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>There is no special case for the null value, it is possible to assign this value to an attribute.<\/p>\n\n\n\n<p>We will set the job attribute to null:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\n\t{ \"op\": \"add\", \"path\": \"\/job\", \"value\": null }\n]<\/code><\/pre>\n\n\n\n<p>The result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n\t\"firstname\": \"Cyril\",\n\t\"job\": null,\n\t\"hobbies\": &#91;\"cooking\"],\n\t\"pets\" : &#91;\n\t\t{\n\t\t\t\"type\": \"cat\",\n\t\t\t\"name\": \"Horu\"\n\t\t}\n,\n\t\t{\n\t\t\t\"type\": \"cat\",\n\t\t\t\"name\": \"chanel\"\n\t\t}\n\t],\n\t\"eye\": \"Green\",\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Limitations_Disadvantages\"><\/span>Limitations \/ Disadvantages<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Unlike <strong>JSON Merge Patch<\/strong>, this format has <strong>no functional limitation<\/strong>. This format allows you to set null values, delete nodes, update tables, etc.<\/p>\n\n\n\n<p>On the other hand, I still find a disadvantage to this format: it is<strong> not very readable<\/strong> as soon as there are a certain number of operations.<\/p>\n\n\n\n<p>At first glance, it <strong>can be difficult to see what is being updated<\/strong> (unlike 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>JSON Patch is a powerful tool for making partial updates to JSON documents. By providing a standardized way of specifying changes to be made to a JSON document, JSON Patch allows for efficient and flexible updates.<\/p>\n\n\n\n<p>From my point of view, JSON Patch stands out from JSON Merge Patch, because the latter is too functionally limited.<\/p>\n\n\n\n<p>Learn more: <a href=\"https:\/\/jsonpatch.com\/\" title=\"\">JSON Patch<\/a><\/p>\n\n\n\n<p>Visit our <a href=\"https:\/\/extendsclass.com\/json-patch.html\" title=\"\">JSON Patch generator<\/a> to do some tests. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short introduction to the JSON Patch format, useful for your REST APIs.<\/p>\n","protected":false},"author":1,"featured_media":77,"comment_status":"open","ping_status":"open","sticky":false,"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\/71"}],"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=71"}],"version-history":[{"count":9,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts\/71\/revisions"}],"predecessor-version":[{"id":79,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts\/71\/revisions\/79"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/media\/77"}],"wp:attachment":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/media?parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/categories?post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/tags?post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}