0

Suppose I have the following json in a file json.txt

{
  "first_name": "John",
  "last_name": "Smith",
  "things_carried": [
    "apples",
    "hat",
    "harmonica"
  ],
  "children": [
    {
      "first_name": "Bobby Sue",
      "last_name": "Smith"
    },
    {
      "first_name": "John Jr",
      "last_name": "Smith"
    }
  ]
}

In shell script I had written the logic to find the size of children array using jq tool .

size=cat json.txt | jq '.children | length'    
i=0

while [ $i -le $size ]    
do
    array[$i]=$(cat json.txt | jq '.children[$i]')
    i=`expr $i + 1`
done

On running this it gives the following error -

.children[$i]          1 compile error

It seems that it is not able to replace the variable i in the children[] array , as because if we give the expression -

array[$i]=$(cat json.txt | jq '.children[0]') 

it runs well .

Can someone help me .

4
  • size=cat json.txt | jq '.children | length' will not do what you want. You need to use command substitution Commented Aug 12, 2015 at 16:32
  • size=cat json.txt | jq '.children | length,it will give the length of children array . Now through a loop I want to assign each json object inside the children[] array to another array variable Commented Aug 12, 2015 at 16:38
  • Please check echo $size to see Commented Aug 12, 2015 at 16:44
  • $size is giving correct value = 2 . The problem lies in the jq tool . If we give shell variable inside the jq tool , jq fails to parse . Commented Aug 12, 2015 at 17:19

3 Answers 3

2

You're using single quotes around the jq program. Shells do not interpolate variables inside single quotes; this is intentional and the jq manual recommends using single quotes around programs for this reason.

An argument syntax is provided by jq for this purpose. This syntax allows you to set jq variables to the value of shell variables. You could replace your current jq invocation with this:

array[$i]=$(cat json.txt | jq --arg i $i '.children[$i | tonumber]')

Sign up to request clarification or add additional context in comments.

3 Comments

--arg would make jq interpret the value of $i as a string. One should use --argjson flag instead for the value of $i to be read
Agreed! In the code above, I use tonumber to work around that. I'm pretty sure that jq 1.5 hadn't been released yet, hence why I didn't used --argjson.
Oh I actually missed the tonumber and yeah I figured that jq 1.5 wasn't probably out then :-)
1

It looks like you're just trying to set the children to a bash array variable.

You don't need to loop, just set the array directly.

$ IFS=$'\n'; array=($(jq -c '.children[]' json.txt))

Comments

-1

You should use the following syntax:

array[$i]=$(cat json.txt | jq '.children['${i}']')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.