Jq: How can I put integer as an argument in jq?

Created on 3 Aug 2016  路  2Comments  路  Source: stedolan/jq

for (( i=1; i<=groupCount; i++ )) ; 
do
    echo $i
    echo $(echo "$input" | jq --arg i $i '.SecurityGroups[$i]')
done

This returns an error:

jq: error (at <stdin>:189): Cannot index array with string "1"

2
jq: error (at <stdin>:189): Cannot index array with string "2"

3
jq: error (at <stdin>:189): Cannot index array with string "3"

Is there any way around this?

Most helpful comment

Either cast the argument to a number (as in .SecurityGroups[$i | tonumber]) or obtain the argument as a JSON value in the first place (as in --argjson i "$i")

All 2 comments

I have the same scenario and am also wondering what a possible solution might be for this.

Either cast the argument to a number (as in .SecurityGroups[$i | tonumber]) or obtain the argument as a JSON value in the first place (as in --argjson i "$i")

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mcandre picture mcandre  路  3Comments

thelonious picture thelonious  路  4Comments

lhunath picture lhunath  路  3Comments

rokka-n picture rokka-n  路  4Comments

tbelaire picture tbelaire  路  4Comments