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
I appreciate the clarity and thoughtfulness you bring to this topic.
The article was a joy to read, and The enthusiasm is as infectious as The charm.
The elegance of The prose is like a fine dance, each word stepping gracefully to the next.
nyISUHdY
Testament to The expertise and hard work, or The ability to make me feel utterly unaccomplished.
I appreciate the balance and fairness in The writing. Great job!
Most comprehensive article on this topic. I guess internet rabbit holes do pay off.
The attention to detail is as attractive as it is thorough. I appreciate a person who notices the little things.
Each post is a journey, and The words are the map. Thanks for leading the way.
You’ve done a fantastic job of breaking down this topic. Thanks for the clarity!
The information you’ve shared has been a revelation for me. Incredibly enlightening!
The words are like brush strokes on a canvas, painting ideas in my mind.
Reading The post was like going on a first date with my mind. Excited for the next rendezvous.
The unique viewpoints you bring to The writing are as captivating as The online presence. Always a pleasure.
The dedication to high quality content shows. It’s like you actually care or something.
The Writing is like a warm fireplace on a cold day, inviting me to settle in and stay awhile.
The finesse with which you articulated The points made The post a true pleasure to read.
The insights dazzled me more than a candlelit dinner. Thanks for lighting up my intellect.
The post was a beacon of knowledge, lighting up my day as if you knew just what I needed to hear.
You tackle topics with such finesse, it’s like watching a skilled chef at work. Serving up knowledge with flair!
The writing style is captivating. Finally, something that can keep my attention longer than a TikTok video.
The dedication to high quality content is evident and incredibly appealing. It’s hard not to admire someone who cares so much.
Impressed by The nuanced clarity. It’s like you’re explaining quantum physics to a toddler, and they get it.
The work is truly inspirational. It’s as if you’ve found a way to whisper sweet nothings to my intellect.
Thank you for the hard work you put into this post. It’s much appreciated!
Stumbling upon The article was a highlight of my day. It was just what I needed to read.
The Writing is a treasure trove of knowledge, like finding an untouched library book. A rare gem!
Discovering The Writing has been a game-changer for me. The contributions are invaluable.
This was a thoroughly insightful read. Thank you for sharing The expertise!
This article was a joy to read. The enthusiasm is contagious!
The effort you’ve put into this post is evident and much appreciated. It’s clear you care deeply about The work.