Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't filter using JMESPath query on integers #11984

Closed
yooakim opened this issue Jan 29, 2020 · 9 comments
Closed

Can't filter using JMESPath query on integers #11984

yooakim opened this issue Jan 29, 2020 · 9 comments
Assignees
Labels
Core CLI core infrastructure
Milestone

Comments

@yooakim
Copy link

yooakim commented Jan 29, 2020

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

@yonzhan yonzhan added the Compute - VM az vm label Jan 30, 2020
@yonzhan yonzhan added the Compute az vm/vmss/image/disk/snapshot label Jan 30, 2020
@yonzhan
Copy link
Collaborator

yonzhan commented Jan 30, 2020

@qwordy please take a look.

@vazach
Copy link

vazach commented Feb 20, 2020

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.

@jiasli
Copy link
Member

jiasli commented Feb 20, 2020

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 {}

@qwordy
Copy link
Member

qwordy commented Mar 19, 2020

@yooakim Is your problem solved?

@yooakim
Copy link
Author

yooakim commented Apr 1, 2020

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

@yooakim yooakim closed this as completed Apr 1, 2020
@rswe
Copy link

rswe commented Aug 24, 2020

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

@mirageglobe
Copy link

mirageglobe commented Aug 28, 2020

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}'

@qwordy qwordy added Core CLI core infrastructure and removed Compute az vm/vmss/image/disk/snapshot Compute - VM az vm labels Sep 7, 2020
@qike-ms
Copy link

qike-ms commented Sep 18, 2020

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.

@anoop7181
Copy link

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

@yonzhan yonzhan added this to the Backlog milestone Dec 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core CLI core infrastructure
Projects
None yet
Development

No branches or pull requests

10 participants