1

I'm attempting the following Azure CLI command and query from PowerShell:

az account list-locations --query [?contains(name,"us")].[displayName,name]

Unfortunately the --query parameter gives me this error:

Missing argument in parameter list

...and it points to the comma in the contains function. That's a surreal error, since the contains function only takes two arguments...and both are present. Furthermore, I have tested the above syntax in the jmespath online fiddle, where it works just fine.

In that online fiddle, I successfully used this expression:

[?contains(state,'WA')].name | sort(@) | {WashingtonCities: join(', ', @)}

The data for the above sample, is as follows:

[
    {"name": "Seattle", "state": "WA"},
    {"name": "New York", "state": "NY"},
    {"name": "Bellevue", "state": "WA"},
    {"name": "Olympia", "state": "WA"}
]

So what could possibly be wrong with --query that I'm passing to the Azure CLI?

I have also attempted this alternate contains syntax someone proposed, which also didn't work.

Brent Arias
  • 29,277
  • 40
  • 133
  • 234

2 Answers2

1

Try back-ticks instead of double-quotes. See the examples of the function contains. The query below should return an empty list because none of the names contains us

[?contains(name, `us`)].[displayName,name]

For example,

[?contains(name, `ll`)].[displayName,name]

should return

[[nul, "Bellevue]"]


Python program for testing

#!/usr/bin/python3.9

import jmespath
import yaml


s = [{"name": "Seattle", "state": "WA"},
     {"name": "New York", "state": "NY"},
     {"name": "Bellevue", "state": "WA"},
     {"name": "Olympia", "state": "WA"}]

print(yaml.dump(s))

r = jmespath.search('[?contains(name, `us`)].[displayName,name]', s)
print(yaml.dump(r))

r = jmespath.search('[?contains(name, `ll`)].[displayName,name]', s)
print(yaml.dump(r))

r = jmespath.search('[?contains(state, `WA`)].name|sort(@)|{WA: join(`, `, @)}', s)
print(yaml.dump(r))

gives

- name: Seattle
  state: WA
- name: New York
  state: NY
- name: Bellevue
  state: WA
- name: Olympia
  state: WA

[]

- - null
  - Bellevue

WA: Bellevue, Olympia, Seattle
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • What shell are you using? I'm using PowerShell Core, and the use of back-ticks causes PowerShell to prompt me with a `>>` continuation; That is, it is now expecting something additional. If I simply hit `enter` at the continuation, then I'm given an error about a missing argument in the `contains` expression. – Brent Arias Mar 27 '23 at 16:23
  • I'm using Bash. The execution of the Python script shouldn't depend on a shell. – Vladimir Botka Mar 27 '23 at 16:56
  • Wow! Although I can't make this work in PowerShell, I just discovered I can make it work in the git bash shell, with this specific use of single-ticks and back-ticks: --query '[?contains(name,`us`)].[displayName,name]'. Remember, I am exercising this JMESpath not from a program...but from a CLI environment by way of the Azure CLI. – Brent Arias Mar 27 '23 at 17:13
0

Testing in Azure Cloud Shell, the following syntax works in both Bash and PowerShell environments.

az account list-locations --query "[?contains(name,'us')].[displayName,name]"

Most Microsoft docs are written and tested for Bash so if you are working in a PowerShell environment, check out one of these docs for common differences: