How do I get a list containing only Azure region names?
Asked Answered
C

2

8

In the Azure Cloud Shell, I can use az account list-locations to get a list of all the locations supported in my subscription. How can I filter the response to only include the name property of the location and exclude all other properties?

Instead of a list of objects like this:

{
"displayName": "UK West",
"id": "<<removed>>",
"latitude": "53.427",
"longitude": "-3.084",
"name": "ukwest",
"subscriptionId": null
}

I want to get collection of names like this:

{
"name": "ukwest",
"name": "ukwest2",
"name": "ukwest3",
}
Chinchy answered 12/7, 2018 at 18:25 Comment(1)
technically your JSON is invalid, it cannot contain duplicate keys. it should be [] - so an array containing strings or objectsVernacularize
V
8

You can use -query parameter for that:

az account list-locations --query '[].name'

its using a jmespath notation.

ps. some examples.

Vernacularize answered 12/7, 2018 at 18:31 Comment(1)
Azure Cloud Shell with PowerShell: Get-AzLocation | select displayname,locationTelfer
P
6

The accepted answer didn't work for me.

I'm using the az CLI version 2.3.1.

This is what worked for me:

az account list-locations --query "sort_by([].{Location:name}, &Location)" -o table

Which yields this result:

Location
------------------
australiacentral
australiacentral2
australiaeast
australiasoutheast
brazilsouth
canadacentral
canadaeast
centralindia
centralus
eastasia
eastus
eastus2
francecentral
francesouth
germanynorth
germanywestcentral
japaneast
japanwest
koreacentral
koreasouth
northcentralus
northeurope
norwayeast
norwaywest
southafricanorth
southafricawest
southcentralus
southeastasia
southindia
switzerlandnorth
switzerlandwest
uaecentral
uaenorth
uksouth
ukwest
westcentralus
westeurope
westindia
westus
westus2

You can find more details on the query language here:

Query Azure CLI command output

Pronation answered 17/4, 2020 at 2:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.