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
Thank you for adding value to the conversation with The insights.
Always learning something new here, because apparently, I didn’t pay enough attention in school.
The writing style is like a signature scent—distinct, memorable, and always pleasant.
Each post you write is like a letter I’ve been waiting for. Always delivered with care.
The expertise and hard work shine through, making me admire you more with each word.
I’m so glad I stumbled upon this article on the state of the country. It was exactly what I needed to read!
The Writing is like a gallery of thoughts, each post a masterpiece worthy of contemplation.
The writing is a constant source of inspiration and knowledge for me on the state of the country. Thank you!
This post is packed with insights, each one a gentle nudge to my intellect and curiosity.
The unique viewpoints in The writing never fail to impress me. Insightful as always!
Shedding light on this subject like you’re the only one with a flashlight. Refreshing to see someone who thinks they have all the answers.
This post has been incredibly helpful, like a guiding hand in a crowded room. The guidance is much appreciated.
Provoked thought and taught me something new, as if my brain needed more exercise.
The piece was both informative and thought-provoking, like a deep conversation that lingers into the night.
The insights on the state of the country add so much value to the conversation. I always learn something new from you.
This piece was beautifully written and incredibly informative about the state of the country. Thank you for sharing!The Writing is like a lighthouse for my curiosity, guiding me through the fog of information.
The post touched on things that resonate with me personally. Thank you for putting it into words.
The unique perspective on this subject was enlightening. It’s refreshing to see someone so passionate about their topic.
You tackled a hard to understand issue with elegance and insight. I feel much more informed after reading The post.
This post is a testament not only to The expertise but also to The dedication. Truly inspiring.
The words are like seeds, planting ideas that blossom into understanding and appreciation.
I find myself lost in The words, much like one would get lost in someone’s eyes. Lead the way, I’m following.
Reading The article was a joy. The enthusiasm for the topic is really motivating.
Reading this gave me a lot of insights. The expertise really shines through, and I’m grateful for it.
The Writing has become like a favorite meeting spot, where great minds and ideas mingle.
You’ve articulated The points with such finesse. Truly a pleasure to read.
The dedication to high quality content shows. It’s like you actually care or something.
The balance and fairness in The writing make The posts a must-read for me. Great job!
The friendly faces at Charlotte Dog Park make every visit special; it’s like a little dog-loving community.
Opened my eyes to new perspectives, and here I was thinking I’d seen it all.
The depth of The research is impressive, almost as much as the way you make hard to understand topics captivating.
Each post is a journey, and The words are the map. Thanks for leading the way.
The Writing is like a trusted compass, always pointing me in the direction of enlightenment.
The ability to convey nuanced ideas with clarity is as alluring as a whispered secret.
Appreciate the balance and fairness, like a judge, but without the gavel.
Thank you for adding value to the conversation with The insights.
Amazed by The knowledge breadth, or what I’ve been mistaking for just good Googling skills.