Azure CLI --query: A Practical JMESPath Guide
Table of Contents
Updated November 2026: Re-checked every query against the JMESPath version Azure CLI 2.93 ships, fixed a broken filter, and added functions, pipes, -o tsv quirks, null handling and quoting rules.
Every Azure CLI command accepts --query. It takes a JMESPath
expression and lets you filter, reshape and sort the JSON that az returns, without piping everything through jq, grep and awk. I still see a lot of shell scripts doing exactly that, so this is my cheat sheet of the queries I use in real scripts, plus the gotchas that bit me along the way.
How --query and -o fit together
az fetches the full response from the Azure REST API, runs your --query expression on it locally, and only then hands the result to the output formatter you picked with -o (--output).
From API response to shell variable
Two things follow from that:
--queryshapes the output; it does not make the API call cheaper. Server-side filters such as-gstill matter.- Output-format quirks (more on
-o tsvbelow) apply to whatever your query returns.
The formats you can pass to -o are json (default), jsonc, yaml, yamlc, table, tsv and none.
Select a single property
To attach an Azure Container Registry to an AKS cluster, you need its resource ID. Combine --query with -o tsv to get the bare string without JSON quotes:
acrId=$(az acr show -n myacr --query id -o tsv)
echo $acrId
# /subscriptions/<SUB_ID>/resourceGroups/rg-sample/providers/Microsoft.ContainerRegistry/registries/myacr
az acr show finds the registry by name, so -g is optional here.
Need more than one value? A multiselect list returns them in a fixed order. With -o tsv, a top-level list prints one value per line:
{ read -r acrId; read -r loginServer; } < <(az acr show -n myacr --query "[id, loginServer]" -o tsv)
Project properties from objects in an array
[] flattens the array the command returns and applies the rest of the expression to every element. To get the IDs of all storage accounts in a resource group:
az storage account list -g rg-sample --query "[].id" -o tsv
Use a multiselect hash ({alias:expression}) to pick several properties and rename them. Dot notation reaches into nested objects such as sku.name:
az storage account list -g rg-sample \
--query "[].{name:name, sku:sku.name, myId:id}" \
-o json
[
{
"name": "stlogs",
"sku": "Standard_LRS",
"myId": "/subscriptions/<SUB_ID>/resourceGroups/rg-sample/providers/Microsoft.Storage/storageAccounts/stlogs"
},
{
"name": "stweb",
"sku": "Standard_GRS",
"myId": "/subscriptions/<SUB_ID>/resourceGroups/rg-sample/providers/Microsoft.Storage/storageAccounts/stweb"
}
]
The alias matters for -o table: the table formatter hides id, type and etag by default. Rename the field (myId:id) and it shows up as a column.
Filter arrays
A filter expression [?condition] keeps every element for which the condition is true:
az storage account list -g rg-sample \
--query "[?accessTier == 'Hot']" \
-o table
Strings go into single quotes ('Hot'). Comparisons are case-sensitive.
Combine conditions
Use &&, || and !, and group with parentheses if needed:
az storage account list -g rg-sample \
--query "[?accessTier == 'Hot' && sku.tier == 'Standard'].name" \
-o tsv
Filter and project in one query
Put the projection right after the filter:
az storage account list -g rg-sample \
--query "[?accessTier == 'Hot' && sku.tier == 'Standard'].{name:name, sku:sku.name, myId:id}" \
-o table
Booleans
Booleans, numbers, null, arrays and objects are JSON literals in JMESPath and go into backticks. Inside a double-quoted Bash string, escape each backtick with a backslash:
az storage account list -g rg-sample \
--query "[?allowBlobPublicAccess == \`false\`].name" \
-o tsv
For true, the property alone is enough, and ! negates it:
az storage account list -g rg-sample --query "[?allowBlobPublicAccess].name" -o tsv
az storage account list -g rg-sample --query "[?!allowBlobPublicAccess].name" -o tsv
The two “false” queries are not the same. == \false`matches onlyfalse, while !allowBlobPublicAccessalso matches accounts where the property isnull` (never set). For security checks I want the second one.
Numbers
Numbers work the same way. Microsoft Defender for Cloud’s secure score controls make a good example:
az security secure-score-controls list \
--query "[?unhealthyResourceCount >= \`1\`].displayName" \
-o tsv
Floating-point numbers work as well:
az security secure-score-controls list \
--query "[?current >= \`3.3\`].displayName" \
-o tsv
If you write '3.3' instead, you compare a number with a string and az fails with a TypeError.
Functions: contains, starts_with, length, join
JMESPath ships a small set of built-in functions . These are the ones I use the most:
# storage accounts whose name starts with "st"
az storage account list -g rg-sample --query "[?starts_with(name, 'st')].name" -o tsv
# accounts with geo-redundant storage (substring match)
az storage account list -g rg-sample --query "[?contains(sku.name, 'GRS')].name" -o tsv
# how many hot accounts are there?
az storage account list -g rg-sample --query "length([?accessTier == 'Hot'])"
# a comma-separated list of names, as one string
az storage account list -g rg-sample --query "join(', ', [].name)" -o tsv
contains works on strings (substring) and on arrays (element). Both contains and starts_with fail with an “invalid type for value: None” error when the field is null on any element; see Handle null values
for the fix.
Sort arrays
sort_by takes an array and an expression reference. The & tells JMESPath to evaluate healthyResourceCount for every element instead of once up front:
az security secure-score-controls list \
--query "sort_by([?current >= \`3.3\`], &healthyResourceCount)[].{name:displayName, healthy:healthyResourceCount}" \
-o table
Wrap it in reverse() for descending order, or use max_by/min_by when you only need the top element:
az security secure-score-controls list \
--query "max_by(@, &weight).displayName" -o tsv
sort_by needs the sort key to be all strings or all numbers. A single null makes the whole query fail.
Pipes and indexes
A projection like [?...].name keeps projecting, so [0] at its end would be applied to every name. A pipe | stops the projection and passes the result on as a whole:
# first hot storage account
az storage account list -g rg-sample --query "[?accessTier == 'Hot'] | [0].name" -o tsv
@ refers to the current value, which is what you pass to functions after a pipe:
az storage account list -g rg-sample --query "[?accessTier == 'Hot'] | length(@)"
Work with nested arrays
Responses get big fast, and the interesting bits usually sit in a nested array. Key Vault access policies are a good example. Note that new vaults use Azure RBAC by default, so this only applies to vaults that still use the access policy model (properties.enableRbacAuthorization is false).
List every principal and its permissions:
az keyvault show -n kv-sample \
--query "properties.accessPolicies[].{objectId:objectId, permissions:permissions}" \
-o json
[
{
"objectId": "<OBJECT_ID>",
"permissions": {
"certificates": [],
"keys": ["get", "list"],
"secrets": ["get", "list"],
"storage": []
}
}
]
Now only the principals that can delete secrets. Filter on the original array first, then project:
az keyvault show -n kv-sample \
--query "properties.accessPolicies[?contains(permissions.secrets, 'delete')].{objectId:objectId, secrets:permissions.secrets}" \
-o json
Two things to watch out for:
- Permission names are stored the way they were set. Policies created with the CLI use
delete, older ones created in the portal may containDelete.containsis case-sensitive, so check both with||if your vaults have a mixed history. - Flatten nested arrays with a second
[]:properties.accessPolicies[].permissions.secrets[]returns one flat list of all secret permissions.
Handle null values
Missing properties come back as null, and most functions reject null. Use || to fall back to a default (|| returns the right side when the left side is null, false or empty):
az keyvault show -n kv-sample \
--query "properties.accessPolicies[?contains(permissions.keys || \`[]\`, 'all')].objectId" \
-o tsv
not_null(permissions.keys, \[]`)does the same. For strings, fall back to’’`.
-o tsv quirks
-o tsv is the format for shell variables, but it has a few rough edges:
nullprints asNone, so check for that string, not for an empty variable.- A boolean on its own prints as
true/false, but inside a row ([].[name, allowBlobPublicAccess]) it prints asTrue/False. Compare case-insensitively or project the boolean on its own. - Nested objects print as an empty field and nested arrays as their length. Project the scalar values you need.
- A list of scalars prints one value per line; a list of lists or objects prints one tab-separated row per element.
- Without a query, columns are ordered by key name, and Microsoft makes no ordering guarantee. Use a multiselect list (
[].[name, location, sku.name]) when you split columns withcutorread.
az storage account list -g rg-sample \
--query "[].[name, location, sku.name]" -o tsv | \
while IFS=$'\t' read -r name location sku; do
echo "$name ($sku) in $location"
done
Quoting in Bash and PowerShell
The expressions above use double quotes for the shell and single quotes for JMESPath strings, so literals need ```. If a query has no string literals, single quotes for the shell save you the escaping:
az account list --query '[?isDefault == `true`].name' -o tsv
In PowerShell the backslash is not an escape character. Double the backtick instead:
az account list --query "[?isDefault == ``true``].name" -o tsv
Recap
JMESPath covers filtering, projections, sorting and null handling, which is everything I used to reach for jq and grep for. I still script everything. I just let az do the JSON work and keep the shell part for the glue.