azure-cli icon indicating copy to clipboard operation
azure-cli copied to clipboard

Can't filter using JMESPath query on integers

Open yooakim opened this issue 6 years ago • 9 comments

I want to retrieve a list of VM sizes where the name contains _B and the number of CPU cores is 4 - so I try this AZ CLI command:

az vm list-sizes --query "sort_by(@,&name)[?contains(name,'_B')]"

which works fine. but when I try to add the filter for number of cores it does not work:

az vm list-sizes --query "sort_by(@,&name)[?contains(name,'_B')] | [?numberOfCores == 4 ]"

I have also tried this:

az vm list-sizes --query "sort_by(@,&name)[?contains(name,'_B')] | [?numberOfCores == '4' ]"

but of course I don't get the expected result as 4 should be an integer.

It seems as if the version of JMESPath used by Azure CLI does not work with integers? Or am I doing something wrong?

Command Name az vm list-sizes

Errors:

az vm list-sizes: error: argument --query: invalid jmespath_type value: "sort_by(@,&name)[?contains(name,'_B')] | [?numberOfCores == 4 ]"

To Reproduce:

Run the following in an Azure CLI command line:

az vm list-sizes --query "sort_by(@,&name)[?contains(name,'_B')] | [?numberOfCores == 4 ]"

Expected Behavior

I expected to get a filtered list where only sizes with numberOfCores = 4 would be listed.

Environment Summary

Linux-4.15.0-1066-azure-x86_64-with-debian-stretch-sid
Python 3.6.5
Shell: bash

azure-cli 2.0.80

Extensions:
aks-preview 0.4.8

Additional Context

yooakim avatar Jan 29 '20 16:01 yooakim

@qwordy please take a look.

yonzhan avatar Jan 30 '20 01:01 yonzhan

I'm running into the same issue and same error message when listing blobs and trying to filter them by content length. So this is really not an issue related to VMs but rather to the JMESPath implementation in CLI.

az storage blob list --query "[?properties.contentLength>0]" --container-name <container-name> --account-name <sa name> --sas-token <token>

also results in the error message described above. Using http://jmespath.org/ I can validate that the query is correct.

vazach avatar Feb 20 '20 11:02 vazach

According to JMESPath specification, for Filter Expressions, numbers need to be quoted by backtick `.

To get a list of VM sizes where the name contains _B and the number of CPU cores is 4, on Windows:

az vm list-sizes -l westus --query "sort_by(@,&name)[?contains(name, `_B`) && numberOfCores == `4`]"
[
  {
    "maxDataDiskCount": 8,
    "memoryInMb": 16384,
    "name": "Standard_B4ms",
    "numberOfCores": 4,
    "osDiskSizeInMb": 1047552,
    "resourceDiskSizeInMb": 32768
  }
]

On Linux, since ` is interpolated by Bash, either escape it with \ or use ' (see this post):

az vm list-sizes -l westus --query "sort_by(@,&name)[?contains(name, \`_B\`) && numberOfCores == \`4\`]"
az vm list-sizes -l westus --query 'sort_by(@,&name)[?contains(name, `_B`) && numberOfCores == `4`]'

To filter out non-empty blobs, on Windows

az storage blob list --query "[?properties.contentLength > `0`]" --container-name {} --account-name {}

On Linux, use either

az storage blob list --query "[?properties.contentLength > \`0\`]" --container-name {}--account-name {}
az storage blob list --query '[?properties.contentLength > `0`]' --container-name {} --account-name {}

jiasli avatar Feb 20 '20 15:02 jiasli

@yooakim Is your problem solved?

qwordy avatar Mar 19 '20 05:03 qwordy

@qwordy - yes, I think it is. Let's close this for now.

yooakim avatar Apr 01 '20 09:04 yooakim

I found the workarounds above did not work for me in my environment: Windows 10 PSVersion 7.0.3 PSEdition Core azure-cli 2.10.1 Extensions: azure-devops 0.18.0

I used this workaround - converted the source value to compare from an integer to a string, so the integer comparison is never needed:

Pre-req: Azure Devops Project: Settings - Repositories - Repo - Set master Branch Policies. Require a minimum number of reviewers - ON (set to 1)

https://jmespath.org/specification.html#built-in-functions to_string function

See --query to_string usage on last line:

az extension add --name azure-devops
$env:AZURE_DEVOPS_EXT_PAT = 'xxxxxxxxxxxxxxxxxx'
$env:AZDO_ORG_SERVICE_URL = 'https://dev.azure.com/yourorg'

az devops configure --defaults organization=$env:AZDO_ORG_SERVICE_URL
$azdoConfig = az devops configure --list --output tsv

$idRepo=az repos list --project "project1" --query "[?name == 'repo'].id" --output tsv

az repos policy list --project "project1" --repository-id $idRepo --branch master --query "[?to_string(settings.minimumApproverCount) == '1'].id" --output table

rswe avatar Aug 24 '20 20:08 rswe

interestingly backticks dont work out always the same.

$ az pipelines build list --definition-ids "1" --tags "something" --query "[?id==\`1234\`].{id:id}"
[
  {
    "id": 1234
  }
]

$ az pipelines build list --definition-ids "1" --tags "something" --query "[?id=='1234'].{id:id}"
[]

$ az pipelines build list --definition-ids "1" --tags "something" --query "[?id==1234].{id:id}"
az pipelines build list: error: argument --query: invalid jmespath_type value: '[?id==1234].{id:id}'

mirageglobe avatar Aug 28 '20 13:08 mirageglobe

Share some learnings:

If the value is non-string, need to use ` instead of ‘ For example: if the value is a boolean: az account list --query "[?isDefault==`true`].name" -o tsv 2>/dev/null

You can find out if the value is string or not by observing if it's quoted.

qike-ms avatar Sep 18 '20 21:09 qike-ms

another learning On windows Powershell , if you are filtering on integer use single quotes for enclosed the query string as below. az network nsg rule list -g rg-name --nsg-name nsg-name -query '[?priority==700]' -o json

anoop7181 avatar Dec 13 '21 16:12 anoop7181