All Products
Search
Document Center

Alibaba Cloud CLI:Extract parameters and tabulate output

Last Updated:Jul 24, 2024

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 true to enable the row number column. The row number starts from 0.

Default value: false.

Scenario

The response parameters of the query operations of Alibaba Cloud are in JSON format.

  1. Run the following command to query information about all ECS instances:

    aliyun ecs DescribeInstances
  2. 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

  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 the rows parameter.

    aliyun ecs DescribeInstances --output cols=RequestId
  2. Sample output:

    RequestId
    ---------
    2B76ECBD-A296-407E-BE17-7E668A609DDA

Example 2

  1. Run the following command to extract the InstanceId and Status parameters that are returned in the preceding scenario. Set rows to Instances.Instance[] because the JMESPath path of the two parameters is Instances.Instance[]. For more information about the JMESPath expressions, see JMESPath Tutorial.

    aliyun ecs DescribeInstances --output cols="InstanceId,Status" rows="Instances.Instance[]"
  2. Sample output:

    InstanceId             | Status
    ----------             | ------
    i-12345678912345678123 | Stopped
    i-abcdefghijklmnopqrst | Running
  3. 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