Gandhar khaladkar
Gandhar khaladkar

Reputation: 134

convert the output from jq to array

I have dynamodb table present in the aws account, I want to filter it based on the certain conditioning and get the result as an array

below is the aws cli query with jq

aws dynamodb scan  --table-name sample-table  | jq .Items | jq 'map(select(.VpcId.S != "None")) ' | jq  .[].VpcId.S

here is the output which I am getting:


"vpc-yyyyyyyyyyyyyyyyy"
"vpc-zzzzzzzzzzzzzzzzz"
"vpc-XXXXXXXXXxxxxxxxx"

I want the output as

[
"vpc-yyyyyyyyyyyyyyyyy",
"vpc-zzzzzzzzzzzzzzzzz",
"vpc-XXXXXXXXXxxxxxxxx"
]

so that I can pass it to the terraform.

below is the output of the actual command

aws dynamodb scan  --table-name sample-table

{
"Items": [
  {
   
    "VpcId": {
      "S": "vpc-yyyyyyyyyyyyyyyyy"
    }
  },
    {
   
    "VpcId": {
      "S": "None"
    }
  },
  {
   
    "VpcId": {
      "S": "vpc-xxxxxxxxxxxxxxxxx"
    }
  },
  }
]
}

Upvotes: 1

Views: 641

Answers (1)

customcommander
customcommander

Reputation: 18921

Replace .[].VpcId.S with map(.VpcId.S). The .[] syntax takes the elements out of the array.


You can combine your different jq filters together in this way:

.Items | map(select(.VpcId.S != "None") | .VpcId.S)

Upvotes: 4

Related Questions