All Products
Search
Document Center

Alibaba Cloud CLI:Extract parameters and tabulate output

Last Updated:Feb 20, 2024

This topic describes how to use Alibaba Cloud Command Line Interface (CLI) to extract desired parameters from JSON response parameters for the query operations of Alibaba Cloud. By default, Alibaba Cloud CLI displays the extracted parameters in tabular format so that they are easy to read.

--output overview

To make the extracted parameters easy to read, Alibaba Cloud CLI provides the --output option to extract desired parameters from response parameters and displays the output in tabular format by default.

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 JSON response parameter.

For example, the DescribeInstances operation of Elastic Compute Service (ECS) returns the InstanceId and Status parameters.

rows

Specifies the JMESPath path where the parameter is located.

Specifies the data source of the table row in the JSON response parameters by using the JMESPath query statement.

num

Set num to true to enable the row number column. The row number starts with 0.

Default value: false.

Scenario

The response parameters of the query operations of Alibaba Cloud are in the JSON format, which makes the parameters hard to read. For example, you can run the following command to query information about all ECS instances.

aliyun ecs DescribeInstances

The system returns parameters in the format similar to the following example:

{
  "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": "ubuntu_16_0402_64_20G_alibase_20171227.vhd",
        "InstanceTypeFamily": "ecs.xn4",
        "VlanId": "",
        "InstanceId": "i-abcdefghijklmnopqrst",
        "Status": "Running",
        //omit some fields
      },
    ]
  }
}

Sample commands

  • Run the following command to extract the RequestId parameter returned in the preceding scenario. You do not need to specify the rows parameter, because this parameter is a root element.

    aliyun ecs DescribeInstances --output cols=RequestId

    The system returns the parameter in the format similar to the following example:

    RequestId
    ---------
    2B76ECBD-A296-407E-BE17-7E668A609DDA
  • Run the following command to extract the InstanceId and Status parameters 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 how to write JMESPath expressions, see JMESPath Tutorial.

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

    The system returns parameters in the format similar to the following example:

    InstanceId             | Status
    ----------             | ------
    i-12345678912345678123 | Stopped
    i-abcdefghijklmnopqrst | Running

    If you want to enable the row number column, set num to true. The parameters are displayed in the format similar to the following example:

    Num | InstanceId             | Status
    --- | ----------             | ------
    0   | i-12345678912345678123 | Stopped
    1   | i-abcdefghijklmnopqrst | Running