1

Hopefully someone could point me in the right direction!

My input:

{
  "accounts": [
    "bob.doe",
    "joe.gomez",
    "bill.murr"
  ]
}

My attempt:

acc=$(cat $file |  jq -r  '.accounts[]' )
echo "$acc test"

Output:

bob.doe
joe.gomez
bill.murr test

It seems to treat all the values in JSON as one array object.

Desired output:

bob.doe test
joe.gomez test
bill.murr test

Any suggestions?

2 Answers 2

3

The variable acc is assigned the entire output of JQ, there's no loop there. And you don't need one either.

$ jq -r '"\(.accounts[]) test"' file
bob.doe test
joe.gomez test
bill.murr test
Sign up to request clarification or add additional context in comments.

1 Comment

how would the code change if you want to do the exact same thing for all elements but the last one? Tried jq -r '"\(.accounts[:-1]) test"' but its not quite correct.
0

thanks this got me on the right path!

sa=$(cat $file |  jq -r  '"\\n \"\(.service_accounts[])\" = [\"\(.namespace)\"]"')

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.