I wanted to loop through json array, I wrote this simple script but I am getting this error
#! /bin/bash
m=$(jq '.items.item | length' try4.json)
for i in $(seq 1 10);
do
jq --raw-output --arg v $i '.items.item[$v].request.text' try4.json | base64 --decode | sed '1q;d' | awk '{print $2}'
done
Error
jq: error (at try4.json:58): Cannot index array with string "1"
jq: error (at try4.json:58): Cannot index array with string "2"
jq: error (at try4.json:58): Cannot index array with string "3"
jq: error (at try4.json:58): Cannot index array with string "4"
jq: error (at try4.json:58): Cannot index array with string "5"
jq: error (at try4.json:58): Cannot index array with string "6"
jq: error (at try4.json:58): Cannot index array with string "7"
jq: error (at try4.json:58): Cannot index array with string "8"
jq: error (at try4.json:58): Cannot index array with string "9"
jq: error (at try4.json:58): Cannot index array with string "10"
CodePudding user response:
Use --argjson
instead of --arg
. That way your variable v
becomes a number in jq
, not a string.
jq --raw-output --argjson v $i '.items.item[$v].request.text' try4.json | base64 --decode | sed '1q;d' | awk '{print $2}'