Azure-cli: JMESPath query when JSON property names have spaces

Created on 24 Aug 2017  路  14Comments  路  Source: Azure/azure-cli

Description

Outline the issue here:
In JMESpath.org website, I'm able to run the following query:
image

Here, there's a space in one of the JSON names "d e". This works in JMESPath website, but a similar thing fails in CLI.

Environment summary

Install Method: How did you install the CLI? (e.g. pip, interactive script, apt-get, Docker, MSI, nightly)
Answer here: Developer setup

CLI Version: What version of the CLI and modules are installed? (Use az --version)
Answer here:
azure-cli (2.0.10+dev)

acr (2.0.8+dev)
acs (2.0.10+dev)
appservice (0.1.10+dev)
backup (1.0.0+dev)
batch (3.0.3+dev)
billing (0.1.3+dev)
cdn (0.0.6+dev)
cloud (2.0.6+dev)
cognitiveservices (0.1.6+dev)
component (2.0.6+dev)
configure (2.0.10+dev)
consumption (0.1.3+dev)
core (2.0.11+dev)
cosmosdb (0.1.10+dev)
dla (0.0.10+dev)
dls (0.0.10+dev)
feedback (2.0.6+dev)
find (0.2.6+dev)
interactive (0.3.6+dev)
iot (0.1.9+dev)
keyvault (2.0.8+dev)
lab (0.0.8+dev)
monitor (0.0.8+dev)
network (2.0.10+dev)
nspkg (3.0.1+dev)
profile (2.0.8+dev)
rdbms (0.0.5+dev)
redis (0.2.7+dev)
resource (2.0.10+dev)
role (2.0.8+dev)
sf (1.0.5+dev)
sql (2.0.7+dev)
storage (2.0.10+dev)
taskhelp (0.1.5+dev)
testsdk (0.1.0+dev)
utility-automation (0.1.1)
vm (2.0.10+dev)

Python (Windows) 3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 16:02:32) [MSC v.1900 64 bit (AMD64)]

Python location 'd:\MabOneSdk\azure-cli\env\Scripts\python.exe'
OS Version: What OS and version are you using?
Answer here:
Windows 10 Anniversary Edition
Shell Type: What shell are you using? (e.g. bash, cmd.exe, Bash on Windows)
Answer here:
cmd.exe

Infrastructure

All 14 comments

Does this occur when using the --query option or some other place you are using the syntax?

Yes, this occurs when we are using the --query option.

@dragonfly91, can you please provide the command line you tried to pass? I'd like to see how you escaped the quotes in the parameter value...

az backup job show --job-id $job_id --vault "$vault_json" --query properties.extendedInfo.propertyBag."Target Storage Account Name"

What happens if you escape the quotes?

For cmd.exe, that would be:

az backup job show --job-id $job_id --vault "$vault_json" --query properties.extendedInfo.propertyBag."""Target Storage Account Name"""

(note the triple quotes)

In general, if you run your command with --debug, it will show you (among a lot of other things :)) the parameters as they are received.

Closing since I was unable to reproduce the issue. Feel free to reopen with additional information if you still have issues getting things to work as expected...

@johanste What was the solution here? I am experiencing the same issue doing below. Success is seen only when the spaces in the RG name are removed

az account list --query [?name=='something with a space'].id

There are two parts to the question:

1) what is the correct jmesquery expression that I want to execute
2) how to I escape things in my shell to make sure that is what the az command sees.

You can use the --debug flag to the command to see what the az command actually saw after the shell had done its interpretation of what you wrote. I happen to run bash on my mac, so this is what I see first in the debug output:

az account list --query [?name=='something with a space'].id

Command arguments: ['account', 'list', '--query', '[?name==something with a space].id', '--debug']

As you can see, the shell "ate" the quotes when it determined that "[?name==something with a space].id" is a single argument. In my case (again, since I'm running bash), I can in enclose the whole argument in quotes:

az account list --query "[?name=='something with a space'].id" --debug to preserve the single quotes:

Command arguments: ['account', 'list', '--query', "[?name=='something with a space'].id", '--debug']

You can learn more about the quoting rules for bash here: https://www.gnu.org/software/bash/manual/html_node/Quoting.html

Wrapping the queried name actually still provided me with issues. The only solution to this for me was escaping the single quotes around the name I was looking for AS WELL as escaping the spaces.

az account list --query [?name==\'something\ with\ a\ space\'].id

Which shell are you using?

I'm also encountering the exact same issue @JamesEarle is (eg: az account list --query "[?name==\"foo bar\"].id" failing ) with the following versions:

harbor@downtown:~$ bash --version
bash --version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
harbor@downtown:~$ az version
az version
{
  "azure-cli": "2.17.0",
  "azure-cli-core": "2.17.0",
  "azure-cli-telemetry": "1.0.6",
  "extensions": {}
}

This is compounded by the poor handling of globbing subscription names which contain spaces for other commands.

Does

az account list --query "[?name=='foo bar'].id"

solve your problem?

@intlabs, what @johanste provides is correct.

To filter on subscription name, you will need to use Raw String Literals which are surrounded by single quotes (').

$ az account list --query "[?name=='Scuffy Playground 0'].id"
[
  "db4a0109-e76d-42c8-bde3-aaca23696425"
]

On the other hand, double quotes (") are for Identifiers, like:

$ az version --query '"azure-cli"'
"2.15.1"

This helps and indeed does let me continue, I'd not fully appreciated the nuances of bash's literal interpretation of ' within " allowing variable substitution which permits their use in this case.

Was this page helpful?
0 / 5 - 0 ratings