azure-cli icon indicating copy to clipboard operation
azure-cli copied to clipboard

JMESPath query when JSON property names have spaces

Open dragonfly91 opened this issue 9 years ago • 14 comments

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

dragonfly91 avatar Aug 24 '17 10:08 dragonfly91

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

tjprescott avatar Aug 24 '17 15:08 tjprescott

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

dragonfly91 avatar Aug 28 '17 10:08 dragonfly91

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

johanste avatar Aug 29 '17 15:08 johanste

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

dragonfly91 avatar Aug 29 '17 19:08 dragonfly91

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.

johanste avatar Aug 29 '17 19:08 johanste

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 avatar Sep 08 '17 02:09 johanste

@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

JamesEarle avatar May 14 '20 20:05 JamesEarle

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

johanste avatar May 14 '20 21:05 johanste

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

JamesEarle avatar May 18 '20 17:05 JamesEarle

Which shell are you using?

johanste avatar May 18 '20 17:05 johanste

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.

intlabs avatar Dec 30 '20 20:12 intlabs

Does

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

solve your problem?

johanste avatar Dec 30 '20 23:12 johanste

@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"

jiasli avatar Dec 31 '20 02:12 jiasli

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.

intlabs avatar Dec 31 '20 04:12 intlabs