jq - return json as bash array

Viewed 10044

My question is similar to this question but I need something more to be done and can't quite figure out how to do.

This is my JSON string

{
  "value": "1"
}
{
  "value": "3"
}
{
  "value": "4"
}

I need to write a script in BASH to output

  • 2 - If the sequence is missing a number
  • 5 - If the sequence isn't missing a number

In the above example, 2 is missing from the json array. And the script should return 2. If 2 is present in the array it should return 5.

I think I may be able to write the logic to increment the number using a while loop in bash, but I am stuck at this point where I can't figure out how to convert this JSON string to bash array with just the value.

Below is the exact command that I used to get the JSON output. This is AWS CLI command to get all the instances from AWS that has a specific TAG.

readarray -t arr < <(aws ec2 describe-instances --region=us-east-1 --filters --filters "Name=tag:NodeType,Values=worker" --query "Reservations[].Instances[].Tags[]" | jq -r '.[] | select(.Key == "NodeNumber") | {value: .Value}')
printf '%s\n' "${arr[@]}"

The above returns me

{
  "value": "1"
}
{
  "value": "3"
}
{
  "value": "4"
}

However, I need to get just the VALUE field "value" as a bash array

4 Answers
Related