Search code examples
jsonazureazure-clijmespath

Need jmespath query to output the id attribute of a filter expression


Here is my output from azure cli with -o json output.

[
  {
"cloudName": "AzureCloud",
"id": "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx",
"isDefault": true,
"name": "CSC Number One",
"state": "Enabled",
"tenantId": "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
"user": {
  "name": "user1@gmail.com",
  "type": "user"
}
  },
{
"cloudName": "AzureCloud",
"id": "AAAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAA",
"isDefault": false,
"name": "CSC Number two",
"state": "Enabled",
"tenantId": "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY",
"user": {
  "name": "user1@gmail.com",
  "type": "user"
  }
 }
]

I need to print the only id so I can run command:

az account list --query [].id

This will print all the id for me i.e xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx and AAAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAA.

Now Id like to print the id only where name = "CSC Number One", which means result should be xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx.


Solution

  • Problem

    • Construct Jmespath query to return the id attribute of a filter expression
    • Filter expression must match a specific name CSC Number One

    Solution

    @|[? @.name == `CSC Number One` ]|[*].id