This topic describe how to filter results by using Alibaba Cloud CLI. The filter function of Alibaba Cloud CLI obtains the information that you want in a tabular output. Specifically, this function allows you to quickly obtain the information you want from the JSON structure data that is returned from the query interface of Alibaba Cloud.
--output overview
To make a more intuitive command output result, Alibaba Cloud CLI provides --output option to extract the fields that you want from the results, and the default tabular
output. The --output option contains the following fields:
field Name | Description | Additional instructions |
---|---|---|
cols | The column name of the table. It must correspond to fields in json data. | For example, the ECS DescribeInstances interface returns the fields InstanceId and Status. |
rows | Specifies the JMESPath path where the filter field is located. | The jmespath query statement specifies the data source of the table row in the json result. |
num | Specifies num=true to enable the row number column, starting with the number 0.
|
The default is num=false .
|
Scenario
Often it can be difficult to find the information you want in the JSON structured
data that is returned by the query interface of Alibaba Cloud. As an example, consider
the following scenario. Assume that you run the following command to query information
about all ECS instances.
aliyun ecs DescribeInstances
The system returns an output similar to the following script:
{
"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
},
Instance": [
{
"ImageId": "ubuntu1404_64_20G_aliaegis_20150325.vhd",
"InstanceTypeFamilyId":"ecs.s3",
"VlanId": "",
"InstanceId": "I-abcdefghijklmnopqrst ",
"Status": "Running",
//omit some fields
},
]
}
}
From the preceding example, you can see that it can be difficult to extract information from the returned output.
Solution
- Run the following command to filter the field RequestId returned in the preceding scenario.
Note Because this field is the root element, you do not need to specify the rows field.
The system displays output similar to the following:aliyun ecs DescribeInstances --output cols=RequestId
RequestId --------- 2B76ECBD-A296-407E-BE17-7E668A609DDA
- Run the following command to filter the fields InstanceId and Status returned in the preceding scenario. The JMESPath path of the two fields is
Instances.Instance[]
. For details about how to write JMESPath, see JMESPath Tutorial.
The system displays output similar to the following:aliyun ecs DescribeInstances --output cols=InstanceId,Status rows=Instances.Instance[]
If you want to output the row number, specifyInstanceId | Status ---------- | ------ i-12345678912345678123 | Stopped i-abcdefghijklmnopqrst | Running
num=true
. The output result is similar to the following:Num | InstanceId | Status --- | ---------- | ------ 0 | i-12345678912345678123 | Stopped 1 | i-abcdefghijklmnopqrst | Running