Search code examples
jmespath

Compare attribute with number literal


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)


Solution

  • 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.