The response parameters of the query operations of Alibaba Cloud are in JSON format. This topic describes how to use Alibaba Cloud CLI to extract parameters from JSON responses. By default, Alibaba Cloud CLI displays the extracted parameters in tabular format.
Description of the --output option
To visually display the extracted parameters, Alibaba Cloud CLI provides the --output
option. You can use the --output option to extract parameters that you need from response parameters. By default, the output is displayed in tabular format.
The following table describes the parameters contained in the --output
option.
Parameter | Description | Additional information |
cols | The column name of the table. The name must be the same as the corresponding parameter in the JSON response. | For example, the DescribeInstances operation of Elastic Compute Service (ECS) returns the InstanceId and Status parameters. |
rows | Specifies the JMESPath path in which the parameter is located. | The JMESPath query statement is used to specify the data source of the table row in the JSON response parameters. |
num | You can set this parameter to | Default value: |
Scenario
The response parameters of the query operations of Alibaba Cloud are in JSON format.
Run the following command to query information about all ECS instances:
aliyun ecs DescribeInstances
Sample response:
{ "PageNumber": 1, "TotalCount": 2, "PageSize": 10, "RequestId": "2B76ECBD-A296-407E-BE17-7E668A609DDA", "Instances": { "Instance": [ { "ImageId": "ubuntu_16_0402_64_20G_alibase_20171227.vhd", "InstanceTypeFamily": "ecs.xn4", "VlanId": "", "InstanceId": "i-12345678912345678123", "Status": "Stopped", //omit some fields }, { "ImageId": "ubuntu_16_0402_64_20G_alibase_20171227.vhd", "InstanceTypeFamily": "ecs.xn4", "VlanId": "", "InstanceId": "i-abcdefghijklmnopqrst", "Status": "Running", //omit some fields } ] } }
Sample commands
Example 1
Run the following command to extract the
RequestId
parameter that is returned in the preceding scenario. This parameter is a root element. In this case, you do not need to specify therows
parameter.aliyun ecs DescribeInstances --output cols=RequestId
Sample output:
RequestId --------- 2B76ECBD-A296-407E-BE17-7E668A609DDA
Example 2
Run the following command to extract the
InstanceId
andStatus
parameters that are returned in the preceding scenario. Set rows toInstances.Instance[]
because the JMESPath path of the two parameters isInstances.Instance[]
. For more information about the JMESPath expressions, see JMESPath Tutorial.aliyun ecs DescribeInstances --output cols="InstanceId,Status" rows="Instances.Instance[]"
Sample output:
InstanceId | Status ---------- | ------ i-12345678912345678123 | Stopped i-abcdefghijklmnopqrst | Running
If you want to enable the row number column, set the num parameter to
true
. Sample output:Num | InstanceId | Status --- | ---------- | ------ 0 | i-12345678912345678123 | Stopped 1 | i-abcdefghijklmnopqrst | Running