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