Home > Back-end >  Create multiple output files based on the contents of JSON
Create multiple output files based on the contents of JSON

Time:04-09

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

CodePudding user response:

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)}'

If you suspect \t to be present in your data and want to delimit with a non-occuring character, use the NULL delimit as

jq -cjr '.[] | (.name, "\u0000", .content.nested)' | 
  awk -F'\0' '{fname = $1".sql"; print $2 > fname; close(fname)}'
  • Related