processing a list of files using jq and saving output to file

I have a list of files like so:

/path/to/dir1/f1.json
/path/to/dir2/f1.json
/path/to/dir1/f12.json
..

the json is like so:

{"score": 0.98}

I would like to use jq to process all these files and produce an output:

/path/to/dir1/f1.json,0.76
/path/to/dir2/f1.json,0.89
/path/to/dir1/f12.json,0.98
..

I know how to use jq on a single file, but would like to automate this for the list of files and produce the above output if this is possible.

Any tips on how to pipe correctly to jq and assemble the output would be great

>Solution :

Use input_filename to get the file names, string interpolation "\(...)" to compose and the -r flag to output text:

jq -r '"\(input_filename),\(.score)"' /path/to/*/*.json
/path/to/dir1/f12.json,0.98
/path/to/dir1/f1.json,0.76
/path/to/dir2/f1.json,0.89

Leave a Reply