{"id":206,"date":"2021-03-29T18:21:00","date_gmt":"2021-03-29T15:21:00","guid":{"rendered":"https:\/\/www.juustila.com\/antti\/?p=206"},"modified":"2021-03-29T19:04:13","modified_gmt":"2021-03-29T16:04:13","slug":"merging-two-json-files","status":"publish","type":"post","link":"https:\/\/www.juustila.com\/antti\/2021\/03\/29\/merging-two-json-files\/","title":{"rendered":"Merging two JSON files"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Having two large JSON files with one common element, how can you merge them? Editing by hand not so nice if the files contain hundreds of elements.<br \/><br \/>For example, you have a large JSON file of locations:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"389\" src=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-2-1024x389.png\" alt=\"{\n  &quot;name&quot;: &quot;Oulu&quot;,\n  &quot;lat&quot;: 65.013784817,\n  &quot;lon&quot;: 25.47209907\n},\" class=\"wp-image-209\" srcset=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-2-1024x389.png 1024w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-2-300x114.png 300w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-2-768x291.png 768w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-2-535x203.png 535w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-2.png 1360w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">And then you also have a large JSON file of location codes for the commune:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"361\" src=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-3-1024x361.png\" alt=\"{\n  &quot;name&quot;: &quot;Oulu&quot;,\n  &quot;code&quot;: &quot;564&quot;\n},\" class=\"wp-image-210\" srcset=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-3-1024x361.png 1024w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-3-300x106.png 300w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-3-768x271.png 768w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-3-535x189.png 535w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-3.png 1360w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">And you would like to merge these into a one JSON file and parse the commune code, name, and location data at one go.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can do this with <a href=\"https:\/\/stedolan.github.io\/jq\/\">jq<\/a> using the following command:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"389\" src=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-4-1024x389.png\" alt=\".\/jq-osx-amd64 --slurpfile file2 codes.json.txt '\n  INDEX($file2[]; .name) as $dict\n  | $dict[.name] as $x\n  | if $x then . + $x else empty end\n'  coords.json.txt &gt; out.json\" class=\"wp-image-211\" srcset=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-4-1024x389.png 1024w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-4-300x114.png 300w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-4-768x291.png 768w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-4-535x203.png 535w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-4.png 1360w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"> And as a result, you get your JSON element merged in one file:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"416\" src=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-5-1024x416.png\" alt=\"{\n  &quot;name&quot;: &quot;Oulu&quot;,\n  &quot;lat&quot;: 65.013784817,\n  &quot;lon&quot;: 25.47209907,\n  &quot;code&quot;: &quot;564&quot;\n},\" class=\"wp-image-212\" srcset=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-5-1024x416.png 1024w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-5-300x122.png 300w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-5-768x312.png 768w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-5-535x217.png 535w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/03\/carbon-5.png 1360w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Much faster than copying and pasting or implementing your own JSON merger.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Having two large JSON files with one common element, how can you merge them? Editing by hand not so nice if the files contain hundreds of elements. For example, you have a large JSON file of locations: And then you also have a large JSON file of location codes for the commune: And you would &hellip; <a href=\"https:\/\/www.juustila.com\/antti\/2021\/03\/29\/merging-two-json-files\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Merging two JSON files&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[2],"tags":[67,66,69],"class_list":["post-206","post","type-post","status-publish","format-standard","hentry","category-coding","tag-jq","tag-json","tag-merge"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/comments?post=206"}],"version-history":[{"count":3,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/206\/revisions"}],"predecessor-version":[{"id":217,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/206\/revisions\/217"}],"wp:attachment":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/media?parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/categories?post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/tags?post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}