Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create multiple output files based on the contents of JSON

I have a json array of objects.I filter it with jq to get data I want:

cat [{'name': 'a', 'content': {'nested': 'important content'}}, ...] >> jq ".[]|select(MY_FILTER)|.name,.content.nested"

How can I write the output to multiple files, each named {.name}.sql and containing {.content.nested}

I’ve tried experimenting with echo, tee, –unbuffered and –raw, but with no success

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you have an array of JSON objects coming-in, you can pipe that to jq and form the resulting file with a tool like awk (note that a simple bash loop could also be used)

Pipe the command or the JSON array of objects to the following pipeline

jq  -cr '.[] | [.name, .content.nested] | join("\t")' | 
  awk -F'\t' '{fname = $1".sql"; print $2 > fname; close(fname)}'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading