Map Certain JSON Fields Into a Concatenated String to Create Filenames

Let’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.

cat some.json \
     | jq -r '.statements | map([.account.number,.endDate,.id] | join("_") + ".pdf")' \
     | in2csv -f json \
     | sed -n '1d;p' \
     | xargs -I{} cp fubar.pdf {};

Let’s break this down into it’s individual pieces.

Sample JSON Input

{
   "statements" : [ {
     "id" : "60090438-83cc-433f-bbf9-8086f4da846f",
     "account" : {
       "number" : "801284289140212"
     },
     "endDate" : "2021-06-30",
     ...
   }
...
]
}

Using JQ To Select A Few Fields to Concatenate for a File Name

The following line will select three properties/fields out of the JSON

jq -r '.statements | map([.account.number,.endDate,.id] | join("_") + ".pdf")'

It will join them by an underscore and then concatenate a .pdf to the end of the file name.

Convert Quoted JQ JSON into CSV via In2Csv

The jq 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.

That is done with the following:

in2csv -f json

Note the use of the -f json since this is a piped command. That will yield this warning, which we can squash next

/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". 

Remove The First Line From the Bash Output

This piped command remove the header labeled a by default:

sed -n '1d;p'

Creating a Dummy Placeholder File Per File Name Generated with Xargs and Cp

Lastly, I want to generate a placeholder file (touch could be used too) per file name. This command achieves that with xargs

xargs -I{} cp fubar.pdf {};

Sample output:

 
 -rw-r--r--@    1 kevincoy  staff     1676399 Jun 29 16:48 182362260357264_2021-03-31_81f1465b-b2d6-43d9-87bd-d6ede8188188.pdf
 -rw-r--r--@    1 kevincoy  staff     1676399 Jun 29 16:48 468913841109837_2021-03-31_043af01f-bd27-42a7-b37d-c7c4b7d1275f.pdf
 -rw-r--r--@    1 kevincoy  staff     1676399 Jun 29 16:48 558664362096984_2021-03-31_287c8418-98bd-4088-9645-b897c40d0ed4.pdf
 -rw-r--r--@    1 kevincoy  staff     1676399 Jun 29 16:48 881961481598397_2021-03-31_14e1e791-3e2d-4275-b27a-bf49cc2999e8.pdf
 -rw-r--r--@    1 kevincoy  staff     1676399 Jun 29 16:48 201869782345979_2021-03-31_a623f37a-dfe7-4748-af2e-6b45dbeb869b.pdf
 -rw-r--r--@    1 kevincoy  staff     1676399 Jun 29 16:48 177636620098765_2021-03-31_4007c4c2-7263-4d58-8a8f-7b5bd6aef802.pdf
 -rw-r--r--@    1 kevincoy  staff     1676399 Jun 29 16:48 812555146927128_2021-03-31_f3a06bd9-efe3-4da1-bd29-6ee93473d920.pdf 

Resources:

  • https://jqplay.org/s/N6TboUkELM
  • https://csvkit.readthedocs.io/en/1.0.2/scripts/in2csv.html
  • https://stackoverflow.com/questions/604864/print-a-file-skipping-the-first-x-lines-in-bash
  • https://unix.stackexchange.com/questions/349252/cp-after-xargs-not-working

241 Comments:

  1. Thank you for consistently producing such high-high quality content.

  2. The Writing has become a go-to resource for me. The effort you put into The posts is truly appreciated.

  3. This is one of the most comprehensive articles I’ve read on this topic. Kudos!

  4. Provoked thought and taught me something new, as if my brain needed more exercise.

  5. The insights are like a fine wine—rich, fulfilling, and leaving me wanting more.

  6. Thanks for the hard work. I could almost see the sweat on the keyboard. Much appreciated!

  7. The insights are as invigorating as a morning run, sparking new energy in my thoughts.

  8. The insights add so much value to the conversation. I always learn something new from you.

  9. A gift for explaining things, making the rest of us look bad.

  10. You’ve articulated The points with such finesse. Truly a pleasure to read.

  11. Discovering The Writing felt like finding the perfect match. The intellect and charm are a rare combo.

  12. This was a thoroughly insightful read. Thank you for sharing The expertise!

  13. Stumbling upon The article was a highlight of my day. It was just what I needed to read.

  14. Thank you for the hard work you put into this post. It’s much appreciated!

  15. Reading The work is like gazing at a masterpiece; every detail contributes to a breathtaking whole.

  16. Appreciate the balance and fairness, like a judge, but without the gavel.

  17. The arguments were as compelling as The online persona. I’m totally sold—and not just on The ideas.

  18. The creativity and intelligence shine through, blinding almost, but I’ll keep my sunglasses handy.

  19. The work is truly inspirational. I appreciate the depth you bring to The topics.

  20. This was a thoroughly insightful read. Thank you for sharing The expertise!

  21. I’m impressed by The ability to convey such nuanced ideas with clarity.

  22. The unique perspective on this subject was enlightening. It’s refreshing to see someone so passionate about their topic.

  23. This article is a perfect blend of informative and entertaining. Well done!

  24. The hard work you put into this post is as admirable as The commitment to high quality. It’s very attractive.

  25. You tackled a hard to understand issue with elegance and insight. I feel much more informed after reading The post.

  26. The article was a delightful read, and The passion shines as brightly as The intellect. Quite the combination!

  27. Always excited to see The posts, like waiting for a message from a crush. Another excellent read!

  28. I’m so grateful for the information you’ve shared. It’s been incredibly enlightening!

  29. The ability to convey nuanced ideas with clarity is as alluring as a whispered secret.

  30. The unique viewpoints you bring to The writing are as captivating as The online presence. Always a pleasure.

  31. A refreshing take on the subject, like a cool breeze on a hot day. I’m all ears for what you have to say next.

  32. You write with such passion and clarity, it’s like listening to a love song for the mind.

  33. The passion for this subject is infectious. Reading The post has inspired me to learn more.

  34. The depth you bring to The topics is like diving into a deep pool, refreshing and invigorating.

  35. I appreciate how you’ve explained things so clearly. It really helped me understand the topic better.

  36. The finesse with which you articulated The points made The post a true pleasure to read.

  37. A masterpiece of writing! You’ve covered all bases with elegance.

  38. Learned a lot from this post, and here I was thinking I knew it all. Thanks for the humble pie.

  39. Testament to The expertise and hard work, or The ability to make me feel utterly unaccomplished.

  40. A constant source of inspiration and knowledge, like a muse but less mythical.

  41. Making hard to understand concepts readable is no small feat. It’s like you know exactly how to tickle my brain.

  42. This was a great read—thought-provoking and informative. Thank you!

  43. The words are like seeds, planting ideas that blossom into understanding and appreciation.

  44. Provoked thought and taught me something new, as if my brain needed more exercise.

  45. The way you break down ideas is like a chef explaining a recipe, making hard to understand dishes seem simple.

  46. The hard work you put into this post is as admirable as The commitment to high quality. It’s very attractive.

  47. Appreciate the balance and fairness, like a judge, but without the gavel.

  48. The research depth is so evident, I almost thought this was a thesis defense.

  49. Provoked thought and taught me something new, as if my brain needed more exercise.

  50. I look forward to The posts because they always offer something valuable. Another great read!

Leave a Reply

Your email address will not be published. Required fields are marked *