1

I'm looking for an 'az' --query syntax that just lists the json fields and not their values.

this will print a table:

az account list --query [*] -o --table

Name    State  IsDefault  TenantId    CloudName

----    -----  ---------  --------    ---------

bob     Enabled True       nnn        AzureCloud

but I don't want the values under the column names, I just want a list of the column names.

dreftymac
  • 31,404
  • 26
  • 119
  • 182
dbroggy
  • 338
  • 4
  • 7

1 Answers1

5

Context

  • MSFT azure cloud shell console (as of 2019-11-17)
  • azure cloud shell az commands with queries

Use-case

  • User_broglock wants a query to return the field-names (and not field-values) from the output result of a cloud shell az account list command

Solution

  • complete command
az account list --query '@|[0]|keys(@)|[*].{"FieldName":@}' --output table
  • demo result
FieldName
-----------
id
name
state
user
isDefault
tenantId
cloudName

Explanation

  • The Azure CLI uses the --query argument to execute a JMESPath query
  • JMESPath supports the keys() function, which returns the names in a JSON name-value-pair object (aka dictionary) (aka associative array) (aka HashTable)
  • After extracting with keys() we return the result as a list of name-value pairs: {"Fieldname":<CurrentItemFromList>} ... this essentially gives us a table with one column.

Diagram

Diagramuu0283bluhoyaz-001aa

See also

dreftymac
  • 31,404
  • 26
  • 119
  • 182
  • @broglock NOTE: The initial `@|` at-sign and pipe is not actually required, however some choose to write queries this way. – dreftymac Nov 17 '19 at 22:21
  • 1
    you're brilliant, thanks! That's EXACTLY what I'm looking for. – dbroggy Nov 18 '19 at 22:38
  • 1
    broglock , you did not upvote or accepted the answer – Kostas Demiris Mar 31 '20 at 12:25
  • Something must have changed in the last several years, I tried that complete command and I just get `'[0]' is not recognized as an internal or external command, operable program or batch file.`. None of the variations suggested work either. Back to the drawing board... – Joe Skeen Apr 26 '23 at 20:47