Compare attribute with number literal

2.2k views Asked by At

I am trying to find all VMs with exactly 2 cores:

$ az vm list-sizes -o json --query "[?numberOfCores == 2]"
az vm list-sizes: error: argument --query: invalid query value: '[?numberOfCores == 2]'

As I suspected an error with the az command, I installed jp directly, but it also gives an error:

$ az vm list-sizes -o json | jp "[?numberOfCores == 2]"
SyntaxError: Invalid token: tNumber
[?numberOfCores == 2]

In the jmespath specification it looks like I have to use the backtick ` character, but instead of an error it just gives no results:

$ az vm list-sizes -o json | jp '[?numberOfCores == `2`]'
[]

This is the (abbreviated) returned json I'm trying to filter:

$ az vm list-sizes -o json | head -n 20
[
  {
    "maxDataDiskCount": 4,
    "memoryInMb": 123,
    "name": "Standard_DS1",
    "numberOfCores": 1,
    "osDiskSizeInMb": 456,
    "resourceDiskSizeInMb": 789
  },
  {
    "maxDataDiskCount": 8,
    "memoryInMb": 123,
    "name": "Standard_DS2",
    "numberOfCores": 2,
    "osDiskSizeInMb": 456,
    "resourceDiskSizeInMb": 789
  }
]

(I obfuscated the sizes because I don't want to be sued by MS for sharing trade secrets or something)

2

There are 2 answers

2
jamesls On BEST ANSWER

Your JMESPath query is correct, but there was an issue with the way the homebrew version of jp was built (it was using an outdated script for building the jp binary). Now the homebrew installed version of jp will always used the signed release binaries from https://github.com/jmespath/jp/releases. These binaries are tested on every commit. You should be able to brew update && brew upgrade jp, or use a binary from https://github.com/jmespath/jp/releases if you're not using homebrew.

0
ImFarhad On

In my case, I was using double quotes with --query flag while executing office365 cli command same as @user3151902 is using .

Note:: Double quotes worked with the string values but throws error with number values.

Wrong Way::

m365 spo list list -o json --query "[?BaseTemplate==`100`]" -u https://{contoso}.sharepoint.com/sites/{siteName}

Right Way::

m365 spo list list -o json --query '[?BaseTemplate==`100`]' -u https://{contoso}.sharepoint.com/sites/{siteName}