|
| 1 | +--- |
| 2 | +title: Output formats for Azure CLI 2.0 |
| 3 | +description: Use --output to format the output of Azure CLI 2.0 commands to tables, lists or json. |
| 4 | +keywords: Azure CLI 2.0, output, format, table, list, json, Linux, Mac, Windows, OS X |
| 5 | +author: allclark |
| 6 | +manager: douge |
| 7 | +ms.date: 02/27/2017 |
| 8 | +ms.topic: article |
| 9 | +ms.prod: azure |
| 10 | +ms.technology: azure |
| 11 | +ms.assetid: 74bdb727-481d-45f7-a44e-15d18dc55483 |
| 12 | +--- |
| 13 | + |
| 14 | +# Output formats for Azure CLI 2.0 commands |
| 15 | + |
| 16 | +Azure CLI 2.0 uses json as its default output option, but offers various ways for you to format the output of any command. Use the `--output` (or `--out` or `-o`) parameter to format the output of the command into one of the output types noted in the following table. |
| 17 | + |
| 18 | +--output | Description |
| 19 | +---------|------------------------------- |
| 20 | +`json` | json string. `json` is the default. |
| 21 | +`jsonc` | colorized json string. |
| 22 | +`table` | table with column headings. |
| 23 | +`tsv` | tab-separated values. |
| 24 | + |
| 25 | +## Using the json option |
| 26 | + |
| 27 | +The following example displays the list of virtual machines in your subscriptions in the default json format. |
| 28 | + |
| 29 | +```azurecli |
| 30 | +az vm list --output json |
| 31 | +``` |
| 32 | + |
| 33 | +The results are in this form (only showing partial output for sake of brevity). |
| 34 | + |
| 35 | +```json |
| 36 | +[ |
| 37 | + { |
| 38 | + "availabilitySet": null, |
| 39 | + "diagnosticsProfile": null, |
| 40 | + "hardwareProfile": { |
| 41 | + "vmSize": "Standard_DS1" |
| 42 | + }, |
| 43 | + "id": "/subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/DemoVM010", |
| 44 | + "instanceView": null, |
| 45 | + "licenseType": null, |
| 46 | + "location": "westus", |
| 47 | + "name": "DemoVM010", |
| 48 | + "networkProfile": { |
| 49 | + "networkInterfaces": [ |
| 50 | + { |
| 51 | + "id": "/subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/demorg1/providers/Microsoft.Network/networkInterfaces/DemoVM010VMNic", |
| 52 | + "primary": null, |
| 53 | + "resourceGroup": "demorg1" |
| 54 | + } |
| 55 | + ] |
| 56 | + }, |
| 57 | + ... |
| 58 | + ... |
| 59 | + ... |
| 60 | +] |
| 61 | +``` |
| 62 | + |
| 63 | +## Using the table option |
| 64 | + |
| 65 | +The table option provides an easy to read set of output, but note that nested objects are not included in the output with the simple `--output table`, unlike the preceding .json example. Using the same example with 'table' output format provides a curated list of most common property values. |
| 66 | + |
| 67 | +```azurecli |
| 68 | +az vm list --out table |
| 69 | +``` |
| 70 | + |
| 71 | +``` |
| 72 | +Name ResourceGroup Location |
| 73 | +----------- --------------- ---------- |
| 74 | +DemoVM010 DEMORG1 westus |
| 75 | +demovm212 DEMORG1 westus |
| 76 | +demovm213 DEMORG1 westus |
| 77 | +KBDemo001VM RGDEMO001 westus |
| 78 | +KBDemo020 RGDEMO001 westus |
| 79 | +``` |
| 80 | + |
| 81 | +You can use the `--query` parameter to customize the properties and columns you want to show in the list output. The following example shows how to select just the VM Name and the Resource Group Name in the `list` command. |
| 82 | + |
| 83 | +```azurecli |
| 84 | +az vm list --query "[].{ resource: resourceGroup, name: name }" -o table |
| 85 | +``` |
| 86 | + |
| 87 | +``` |
| 88 | +Resource Name |
| 89 | +---------- ----------- |
| 90 | +DEMORG1 DemoVM010 |
| 91 | +DEMORG1 demovm212 |
| 92 | +DEMORG1 demovm213 |
| 93 | +RGDEMO001 KBDemo001VM |
| 94 | +RGDEMO001 KBDemo020 |
| 95 | +``` |
| 96 | + |
| 97 | +## Using the tsv option |
| 98 | + |
| 99 | +'tsv' output format returns a simple text-based and tab-separated output with no headings and dashes. This format makes it easy to consume the output into other commands and tools that need to process the text in some form. Using the preceding example with the `tsv` option outputs the tab-separated result. |
| 100 | + |
| 101 | +```azurecli |
| 102 | +az vm list --out tsv |
| 103 | +``` |
| 104 | + |
| 105 | +``` |
| 106 | +None None /subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/DemoVM010 None None westus DemoVM010 None Succeeded DEMORG1 None Microsoft.Compute/virtualMachines cbd56d9b-9340-44bc-a722-25f15b578444 |
| 107 | +None None /subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/demovm212 None None westus demovm212 None Succeeded DEMORG1 None Microsoft.Compute/virtualMachines 4bdac85d-c2f7-410f-9907-ca7921d930b4 |
| 108 | +None None /subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/demovm213 None None westus demovm213 None Succeeded DEMORG1 None Microsoft.Compute/virtualMachines 2131c664-221a-4b7f-9653-f6d542fbfa34 |
| 109 | +None None /subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/RGDEMO001/providers/Microsoft.Compute/virtualMachines/KBDemo001VM None None westus KBDemo001VM None Succeeded RGDEMO001 None Microsoft.Compute/virtualMachines 14e74761-c17e-4530-a7be-9e4ff06ea74b |
| 110 | +None None /subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/RGDEMO001/providers/Microsoft.Compute/virtualMachines/KBDemo02None None westus KBDemo020 None Succeeded RGDEMO001 None Microsoft.Compute/virtualMachinesed36baa9-9b80-48a8-b4a9-854c7a858ece |
| 111 | +``` |
| 112 | + |
| 113 | +The next example shows how the `tsv` output can be piped to commands like `grep` and `cut` to further parse specific values out of the `list` output. The `grep` command selects only items that have text "RGD" in them and then the `cut` command selects only the eighth field (separated by tabs) value to show in the output. |
| 114 | + |
| 115 | +```azurecli |
| 116 | +az vm list --out tsv | grep RGD | cut -f8 |
| 117 | +``` |
| 118 | + |
| 119 | +``` |
| 120 | +KBDemo001VM |
| 121 | +KBDemo020 |
| 122 | +``` |
| 123 | + |
| 124 | +## Setting the default output format |
| 125 | + |
| 126 | +You can use the `az configure` command to set up your environment or establish preferences such as default settings for output formats. For common use, the easiest output format default is the "table" format - select **3** when prompted for output format choices. |
| 127 | + |
| 128 | +``` |
| 129 | +What default output format would you like? |
| 130 | + [1] json - JSON formatted output that most closely matches API responses |
| 131 | + [2] jsonc - Colored JSON formatted output that most closely matches API responses |
| 132 | + [3] table - Human-readable output format |
| 133 | + [4] tsv - Tab and Newline delimited, great for GREP, AWK, etc. |
| 134 | +Please enter a choice [3]: |
| 135 | +``` |
0 commit comments