How to create for-loops with jq in bash How to create for-loops with jq in bash bash bash

How to create for-loops with jq in bash


While the above answers are correct, note that interpolating shell variables in jq scripts is a terrible idea for all but the most trivial of scripts. On any of the solutions provided, replace the following:

jq ".results[$i]"

With the following:

jq --arg i "$i" '.results[$i | tonumber]'


Try

for i in {0..24}; do cat r1.json | jq ".results[$i]" >> $i.json; done

Note that shell variables can't be expanded inside of single-quotes.

IHTH


The single quotes are probably what is messing you up. Bash variables are not expanded in single quotes. You are passing a literal string .results[$i] to jq. Try double quotes instead:

for i in {0..24}; do    cat r1.json | jq ".results[$i]" >> $i.jsondone