Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use AWS CLI --query option to filter a 1-dimensional array

Tags:

aws-cli

Is it possible to use the --query option of the AWS CLI (described here) to filter a 1 dimensional array? All the examples I can find on the AWS site work great for maps -- but I couldn't figure out the syntax for a simple array.

Consider the dynamodb list-tables command which has a string array in the output:

aws dynamodb list-tables

Sample Output:

{
    "TableNames": [
        "Questions", 
        "Answers",
        "Votes"
    ]
}

Let's say I want to figure out if TableNames contains "Answers". Syntax I've tried that's invalid or doesn't work.

aws dynamodb list-tables --query 'TableNames[?==`Answers`]'
aws dynamodb list-tables --query 'TableNames[?.==`Answers`]'
aws dynamodb list-tables --query '[?TableNames[*]==`Answers`]'
like image 278
Jamey Avatar asked Nov 05 '15 23:11

Jamey


Video Answer


1 Answers

To refer to the current element in an array, you can use the @ character:

$ aws dynamodb list-tables  --query "TableNames[? @ == 'Answers' ]"
[
    "Answers"
]

If you just need a true/false answer of "does this table name exist in the list of tables" you can also use:

$ aws dynamodb list-tables  --query "contains(TableNames, 'Answers')"
true
like image 121
jamesls Avatar answered Sep 28 '22 07:09

jamesls