3

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)

user3151902
  • 3,154
  • 1
  • 19
  • 32

2 Answers2

3

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.

jamesls
  • 5,428
  • 1
  • 28
  • 17
  • I updated my `jp` and it's working now. That's not the first place where I would have looked, thanks a lot! – user3151902 Sep 12 '17 at 16:46
  • 1
    Hi jamesls, I'm wondering why the jmespath need to use the backtick '\`' character for number literals -- e.g., what would the `"[?numberOfCores == 2]"` be interpreted if not comparing `numberOfCores` to exactly 2 cores? what would jmespath think that "2" is? thx – xpt Dec 24 '21 at 21:26
2

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}
ImFarhad
  • 2,669
  • 2
  • 18
  • 30