Azure-cli: query operator with JMESPath doesn't handle a 'dash' (-) properly

Created on 23 Aug 2018  路  3Comments  路  Source: Azure/azure-cli

If a tag on a resource has a name that includes a - the query fails incorrectly.

To Reproduce

  1. Assign a tag of CREATED-BY to a resource group in Azure with a value of 'foobar'
  2. Run the Query
    az group list -o tsv --query "[?tags.CREATED-BY == 'foobar']"

Expected behavior
Normally the results will show the item.

Actual result
The error shows as

az group list: error: argument --query: invalid jmespath_type value: "[?tags.CREATED-BY == 'foobar']"

If you run a similar query on http://jmespath.org/ as below, it works fine

locations[?state == 'WA' && foo-bar == 'foobar'].name | sort(@)

Using data of:

{
  "locations": [
    {"name": "Seattssle", "state": "WA", "foo-bar": "foobar"},
    {"name": "New York", "state": "NY"},
    {"name": "Bellevue", "state": "WA"},
    {"name": "Olympia", "state": "WA"}
  ]
}

Environment summary
This is verified to fail in the Azure Cloud Shell.

Additional context
Add any other context about the problem here.

Infrastructure question

Most helpful comment

Sorry for running into such troubles, but Jemspath requires the property be a valid identifier unless you wrap them with quotes . Because of the -, CREATED-BY is not a right id, so please wrap it.

(env) D:\sdk\azure-cli>az group list -o tsv --query "[?tags.\"CREATED-BY\" == 'yugangw']"
/subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/resourceGroups/yg4  westus  None    yg4

@tjprescott, @williexu, I am closing this, but if we receive more such confusions, we need to document of how to use jmespath.

All 3 comments

same here :-(

Sorry for running into such troubles, but Jemspath requires the property be a valid identifier unless you wrap them with quotes . Because of the -, CREATED-BY is not a right id, so please wrap it.

(env) D:\sdk\azure-cli>az group list -o tsv --query "[?tags.\"CREATED-BY\" == 'yugangw']"
/subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/resourceGroups/yg4  westus  None    yg4

@tjprescott, @williexu, I am closing this, but if we receive more such confusions, we need to document of how to use jmespath.

JFYI, the grammar syntax is here

Was this page helpful?
0 / 5 - 0 ratings