Updated on 2022-07-07 GMT+08:00

How Do I Use cli-json-filter?

By default, results are returned in JSON format when you call cloud service APIs using KooCLI. KooCLI supports the --cli-json-filter parameter to perform JMESPath query on JSON results. This option facilitates the extraction of key information in the results. For example:

By default, the original calling result is output in JSON format:
hcloud ECS NovaListServers --cli-region="ap-southeast-1" --project_id="0dd8cb****************19b5a84546"
{
  "servers": [
    {
      "name": "ecs-a6b4",
      "links": [
        {
          "rel": "self",
          "href": "https://ecs.ap-southeast-1.myhuaweicloud.com/v2.1/0dd8cb****************19b5a84546/servers/4f06****-****-****-****-****04dd856a"
        },
        {
          "rel": "bookmark",
          "href": "https://ecs.ap-southeast-1.myhuaweicloud.com/0dd8cb****************19b5a84546/servers/4f06****-****-****-****-****04dd856a"
        }
      ],
      "id": "4f06****-****-****-****-****04dd856a"
    },
    {
      "name": "hdn-docker",
      "links": [
        {
          "rel": "self",
          "href": "https://ecs.ap-southeast-1.myhuaweicloud.com/v2.1/0dd8cb****************19b5a84546/servers/6731****-****-****-****-****0bc463f0"
        },
        {
          "rel": "bookmark",
          "href": "https://ecs.ap-southeast-1.myhuaweicloud.com/0dd8cb****************19b5a84546/servers/6731****-****-****-****-****0bc463f0"
        }
      ],
      "id": "6731****-****-****-****-****0bc463f0"
    },
    {
      "name": "ecs-8f88",
      "links": [
        {
          "rel": "self",
          "href": "https://ecs.ap-southeast-1.myhuaweicloud.com/v2.1/0dd8cb****************19b5a84546/servers/06a2****-****-****-****-****c79a1a26"
        },
        {
          "rel": "bookmark",
          "href": "https://ecs.ap-southeast-1.myhuaweicloud.com/0dd8cb****************19b5a84546/servers/06a2****-****-****-****-****c79a1a26"
        }
      ],
      "id": "06a2****-****-****-****-****c79a1a26"
    }
  ]
}

Use --cli-json-filter to perform JMESPath query on the original JSON result, obtain the id and name of each servers element, and rename them EcsID and EcsName, respectively. The following is an example:

hcloud ECS NovaListServers --cli-region="ap-southeast-1" --project_id="0dd8cb****************19b5a84546" --cli-json-filter="servers[].{EcsID:id,EcsName:name}"
[
  {
    "EcsID": "4f06****-****-****-****-****04dd856a",
    "EcsName": "ecs-a6b4"
  },
  {
    "EcsID": "6731****-****-****-****-****0bc463f0",
    "EcsName": "hdn-docker"
  },
  {
    "EcsID": "06a2****-****-****-****-****c79a1a26",
    "EcsName": "ecs-8f88"
  }
]

--cli-json-filter can also be used in system commands. For example, to query all custom parameters of the profile test, run the following commands:

hcloud configure list --cli-custom=true --cli-json-filter="profiles[?name=='test'].custom"
[
  {
    "password": {
      "isEncrypted": true,
      "value": "****"
    },
    "projectId": {
      "isEncrypted": false,
      "value": "06810000000000000000000000f89d2e"
    }
  }
]

For more information about how to use --cli-json-filter, see What Are the Precautions for Using cli-json-filter?

How Do I Use cli-json-filter? What Are the Precautions? FAQs

more