{"id":991,"date":"2022-02-01T01:50:28","date_gmt":"2022-02-01T06:50:28","guid":{"rendered":"https:\/\/www.brunerd.com\/blog\/?p=991"},"modified":"2022-02-02T00:24:26","modified_gmt":"2022-02-02T05:24:26","slug":"merging-json-objects-with-jpt-1-0","status":"publish","type":"post","link":"https:\/\/www.brunerd.com\/blog\/2022\/02\/01\/merging-json-objects-with-jpt-1-0\/","title":{"rendered":"merging JSON objects with jpt 1.0"},"content":{"rendered":"\n<p>A cool new feature for <a href=\"https:\/\/github.com\/brunerd\/jpt\" target=\"_blank\" rel=\"noreferrer noopener\">jpt<\/a> 1.0 is the ability perform additional merging operations beyond JSON Merge Patch (RFC 7396). To start with let&#8217;s check out a JSON Merge Patch operation, using the <code>mergepatch<\/code> operation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#key \"a\" is overwritten with the -v value, \"b\" is removed because null\n% jpt -i0 -o <strong>mergepatch<\/strong> -v '{\"a\":\"\ud83d\udfe2\",\"b\":null}' &lt;&lt;&lt; '{\"a\":\"\ud83d\udd34\",\"b\":\"\ud83d\udfe1\"}'                                                                           \n{\"a\":\"\ud83d\udfe2\"}\n\n# use -u to encode all characters above 0x7E with \\u escaping\n% jpt -u -i0 -o <strong>mergepatch<\/strong> -v '{\"a\":\"\ud83d\udfe2\",\"b\":null}' &lt;&lt;&lt; '{\"a\":\"\ud83d\udd34\",\"b\":\"\ud83d\udfe1\"}'                                                                           \n{\"a\":\"\\ud83d\\udfe2\"}<\/code><\/pre>\n\n\n\n<p>Only one key survives, since the Merge Patch spec says that any key with a null value will delete that key in the input JSON. Now perhaps you might want null to survive, if so, use the <code>merge<\/code> operation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>jpt -i0 -o <strong>merge<\/strong> -v '{\"a\":\"\ud83d\udfe2\",\"b\":<strong>null<\/strong>}' &lt;&lt;&lt; '{\"a\":\"\ud83d\udd34\",\"b\":\"\ud83d\udfe1\"}' \n{\"a\":\"\ud83d\udfe2\",\"b\":<strong>null<\/strong>}<\/code><\/pre>\n\n\n\n<p>Now for the more advanced modes. <code>-o merge0<\/code> will only merge in keys that <strong>do not<\/strong> exist. Existing keys remain untouched. To <strong>order<\/strong> the keys alphabetically use <code>-O<\/code> (the letter O)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>% jpt -O -o merge0 -v '{\"common\":\"\ud83d\udfe2\",\"uniqToMergeData\":\"\ud83d\udfe2\"}' &lt;&lt;&lt; '{\"common\":\"\ud83d\udd34\",\"uniqToSource\":\"\ud83d\udd34\"}'\n{\n  \"common\": \"\ud83d\udd34\",\n  \"uniqToMergeData\": \"\ud83d\udfe2\",\n  \"uniqToSource\": \"\ud83d\udd34\"\n}<\/code><\/pre>\n\n\n\n<p>As you can see only the <code>uniqToMergeData<\/code> key comes through, the other two keys <code>common<\/code> and <code>uniqToSource<\/code> do not change. Useful if you are upgrading an object to a newer format and want to retain existing key values and only add the new keys with empty or default values. <\/p>\n\n\n\n<p><code>merge1<\/code> let&#8217;s us do the <em>inverse<\/em> of <code>merge0<\/code> and merge only the keys that <strong>exist<\/strong> in the target JSON. Keys that do not exist do not merge over.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>% jpt -o merge1 -v '{\"uniqToMergeData\":\"\ud83d\udfe2\",\"common\":\"\ud83d\udfe2\"}' &lt;&lt;&lt; '{\"uniqToSource\":\"\ud83d\udd34\",\"common\":\"\ud83d\udd34\"}'\n{\n  \"uniqToSource\": \"\ud83d\udd34\",\n  \"common\": \"\ud83d\udfe2\"\n}<\/code><\/pre>\n\n\n\n<p>If you like pictures (I sure do) and are a visual learner you can think of <code>merge0<\/code> and <code>merge1<\/code> like the <a rel=\"noreferrer noopener\" href=\"https:\/\/www.photoshopessentials.com\/basics\/shapes\/add-subtract\/\" target=\"_blank\">Exclude and Intersect Shape Areas<\/a> in Photoshop.<\/p>\n\n\n\n<p>I hope these new merge modes could be useful to you, if you have a Mac (or *nix with jsc installed) download <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/brunerd\/jpt\" target=\"_blank\">jpt<\/a> from my GitHub <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/brunerd\/jpt\/releases\" target=\"_blank\">Releases<\/a> page and give it a go!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A cool new feature for jpt 1.0 is the ability perform additional merging operations beyond JSON Merge Patch (RFC 7396). To start with let&#8217;s check out a JSON Merge Patch operation, using the mergepatch operation: Only one key survives, since the Merge Patch spec says that any key with a null value will delete that [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37,51,40],"tags":[],"class_list":["post-991","post","type-post","status-publish","format-standard","hentry","category-jpt","category-json","category-projects"],"_links":{"self":[{"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/posts\/991","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/comments?post=991"}],"version-history":[{"count":7,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/posts\/991\/revisions"}],"predecessor-version":[{"id":1083,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/posts\/991\/revisions\/1083"}],"wp:attachment":[{"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/media?parent=991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/categories?post=991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/tags?post=991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}