{"id":640,"date":"2021-06-29T14:36:45","date_gmt":"2021-06-29T14:36:45","guid":{"rendered":"http:\/\/kevinmichaelcoy.com\/blog\/?p=640"},"modified":"2021-06-30T13:49:56","modified_gmt":"2021-06-30T13:49:56","slug":"map-certain-json-fields-into-a-concatenated-string-to-create-filenames","status":"publish","type":"post","link":"http:\/\/kevinmichaelcoy.com\/blog\/2021\/06\/29\/map-certain-json-fields-into-a-concatenated-string-to-create-filenames\/","title":{"rendered":"Map Certain JSON Fields Into a Concatenated String to Create Filenames"},"content":{"rendered":"\n<p>Let&#8217;s say you have a JSON file and in it is an array of values. Each value has a few properties you need to pick from to ultimately create a file name. Here is an example using bash, jq, in2csv and xargs that can be strung together in one line to create a file based on each concatenated filename.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">cat some.json \\\n     | jq -r '.statements | map([.account.number,.endDate,.id] | join(\"_\") + \".pdf\")' \\\n     | in2csv -f json \\\n     | sed -n '1d;p' \\\n     | xargs -I{} cp fubar.pdf {};<\/code><\/pre>\n\n\n\n<p>Let&#8217;s break this down into it&#8217;s individual pieces.<\/p>\n\n\n\n<h2>Sample JSON Input<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">{\n   \"statements\" : [ {\n     \"id\" : \"60090438-83cc-433f-bbf9-8086f4da846f\",\n     \"account\" : {\n       \"number\" : \"801284289140212\"\n     },\n     \"endDate\" : \"2021-06-30\",\n     ...\n   }\n...\n]\n}<\/code><\/pre>\n\n\n\n<h2>Using JQ To Select A Few Fields to Concatenate for a File Name<\/h2>\n\n\n\n<p>The following line will select three properties\/fields out of the JSON<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">jq -r '.statements | map([.account.number,.endDate,.id] | join(\"_\") + \".pdf\")'<\/code><\/pre>\n\n\n\n<p>It will <code>join<\/code> them by an underscore and then concatenate a <code>.pdf<\/code> to the end of the file name.<\/p>\n\n\n\n<h2>Convert Quoted JQ JSON into CSV via In2Csv<\/h2>\n\n\n\n<p>The <code>jq<\/code> command will provide quoted output. Since, there is only one value per item in the array due to the concatenation, I just want to strip out the JSON quoting.<\/p>\n\n\n\n<p>That is done with the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">in2csv -f json<\/code><\/pre>\n\n\n\n<p>Note the use of the <code>-f json<\/code> since this is a piped command. That will yield this warning, which we can squash next<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">\/usr\/local\/Cellar\/csvkit\/1.0.5_1\/libexec\/lib\/python3.8\/site-packages\/agate\/utils.py:276: UnnamedColumnWarning: Column 0 has no name. Using \"a\". <\/code><\/pre>\n\n\n\n<h2>Remove The First Line From the Bash Output<\/h2>\n\n\n\n<p>This piped command remove the header labeled <code>a<\/code> by default:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">sed -n '1d;p'<\/code><\/pre>\n\n\n\n<h2>Creating a Dummy Placeholder File Per File Name Generated with Xargs and Cp<\/h2>\n\n\n\n<p>Lastly, I want to generate a placeholder file (<code>touch<\/code> could be used too) per file name. This command achieves that with <code>xargs<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">xargs -I{} cp fubar.pdf {};<\/code><\/pre>\n\n\n\n<p>Sample output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\"> \n -rw-r--r--@&nbsp; &nbsp; 1 kevincoy&nbsp; staff &nbsp; &nbsp; 1676399 Jun 29 16:48 182362260357264_2021-03-31_81f1465b-b2d6-43d9-87bd-d6ede8188188.pdf\n -rw-r--r--@&nbsp; &nbsp; 1 kevincoy&nbsp; staff &nbsp; &nbsp; 1676399 Jun 29 16:48 468913841109837_2021-03-31_043af01f-bd27-42a7-b37d-c7c4b7d1275f.pdf\n -rw-r--r--@&nbsp; &nbsp; 1 kevincoy&nbsp; staff &nbsp; &nbsp; 1676399 Jun 29 16:48 558664362096984_2021-03-31_287c8418-98bd-4088-9645-b897c40d0ed4.pdf\n -rw-r--r--@&nbsp; &nbsp; 1 kevincoy&nbsp; staff &nbsp; &nbsp; 1676399 Jun 29 16:48 881961481598397_2021-03-31_14e1e791-3e2d-4275-b27a-bf49cc2999e8.pdf\n -rw-r--r--@&nbsp; &nbsp; 1 kevincoy&nbsp; staff &nbsp; &nbsp; 1676399 Jun 29 16:48 201869782345979_2021-03-31_a623f37a-dfe7-4748-af2e-6b45dbeb869b.pdf\n -rw-r--r--@&nbsp; &nbsp; 1 kevincoy&nbsp; staff &nbsp; &nbsp; 1676399 Jun 29 16:48 177636620098765_2021-03-31_4007c4c2-7263-4d58-8a8f-7b5bd6aef802.pdf\n -rw-r--r--@&nbsp; &nbsp; 1 kevincoy&nbsp; staff &nbsp; &nbsp; 1676399 Jun 29 16:48 812555146927128_2021-03-31_f3a06bd9-efe3-4da1-bd29-6ee93473d920.pdf <\/code><\/pre>\n\n\n\n<p>Resources: <\/p>\n\n\n\n<ul><li>https:\/\/jqplay.org\/s\/N6TboUkELM<\/li><li>https:\/\/csvkit.readthedocs.io\/en\/1.0.2\/scripts\/in2csv.html<\/li><li>https:\/\/stackoverflow.com\/questions\/604864\/print-a-file-skipping-the-first-x-lines-in-bash<\/li><li>https:\/\/unix.stackexchange.com\/questions\/349252\/cp-after-xargs-not-working<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say you have a JSON file and in it is an array of values. Each value has a few properties you need to pick from to ultimately create a file name. Here is an example using bash, jq, in2csv and xargs that can be strung together in one line&#8230;<\/p>\n<p class=\"continue-reading-button\"> <a class=\"continue-reading-link\" href=\"http:\/\/kevinmichaelcoy.com\/blog\/2021\/06\/29\/map-certain-json-fields-into-a-concatenated-string-to-create-filenames\/\">Continue reading<i class=\"crycon-right-dir\"><\/i><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,3],"tags":[282,430,429,431,428],"_links":{"self":[{"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts\/640"}],"collection":[{"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/comments?post=640"}],"version-history":[{"count":7,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts\/640\/revisions"}],"predecessor-version":[{"id":649,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts\/640\/revisions\/649"}],"wp:attachment":[{"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/media?parent=640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/categories?post=640"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/tags?post=640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}