All Products
Search
Document Center

CloudOps Orchestration Service:jq

Last Updated:Nov 26, 2024

Overview

jq is a powerful CLI tool that is used to process JSON data. You can use jq to filter, convert, and process data in JSON files or input streams in a simple and efficient manner. jq is particularly useful for processing JSON data based on its readable syntax and bountiful features. This topic describes how to use the ValueSelector parameter of Outputs to parse the output of a CloudOps Orchestration Service (OOS) task in a template. For more information about the syntax, see Tasks.

Official website of jq: https://jqlang.github.io/jq/manual/

Online playground for jq: https://jqplay.org/

Basic usage

Filter specific fields

FormatVersion: OOS-2019-06-01
Tasks:
- Name: describeInstances
  Action: ACS::ExecuteAPI
  Properties:
    Service: ECS
    API: DescribeInstances # API reference: https://www.alibabacloud.com/help/zh/ecs/developer-reference/api-ecs-2014-05-26-describeinstances
    Parameters:
      RegionId: cn-hangzhou
  Outputs:
    instanceIds:
      Type: List # Filter all instance IDs.
      ValueSelector: Instances.Instance[].InstanceId
    instanceId:
      Type: String # Filter the ID of a single instance.
      ValueSelector: Instances.Instance[].InstanceId
    instanceNames:
      Type: List # Filter all instance names.
      ValueSelector: Instances.Instance[].InstanceName
    instanceIdByCpu:
      Type: String # Filter all instance IDs and sort them based on the number of CPU cores.
      ValueSelector: .Instances.Instance | sort_by(.Cpu) |.[].InstanceId
    instanceIdByCreationTime:
      Type: String # Filter all instance IDs and sort them based on the creation time.
      ValueSelector: .Instances.Instance | sort_by(.CreationTime) |.[].InstanceId

Filter data in the specified JSON format

- Name: queryDisks
  Action: 'ACS::ExecuteAPI'
  Properties:
    Service: ECS
    API: DescribeDisks # API reference: https://www.alibabacloud.com/help/zh/ecs/developer-reference/api-ecs-2014-05-26-describedisks
    Parameters:
      RegionId: cn-hangzhou
  Outputs:
    dataDisks:
      Type: Json # Filter the information about data disks.
      ValueSelector: '.Disks.Disk|map( select(.Type == "data")|{"Category":(.Category),"Device": (.Device),"Size":(.Size),"DiskName":(.DiskName),"PerformanceLevel":(.PerformanceLevel)})'
    systemDisk:
      Type: Json # Filter the information about the system disk.
      ValueSelector: '.Disks.Disk|map( select(.Type == "system")|{"Category":(.Category),"Device": (.Device),"Size":(.Size),"DiskName":(.DiskName),"PerformanceLevel":(.PerformanceLevel)})'

Advanced usage

Replace tag keys

FormatVersion: OOS-2019-06-01
Tasks:
- Name: describeInstances
  Action: ACS::ExecuteAPI
  Properties:
    Service: ECS
    API: DescribeInstances # API reference: https://www.alibabacloud.com/help/zh/ecs/developer-reference/api-ecs-2014-05-26-describeinstances
    Parameters:
      RegionId: cn-hangzhou
  Outputs:
    tags:
      Type: List # Filter the tags that do not start with acs and replace the values of these tags from TagKey to Key. 
      ValueSelector: '.Instances.Instance[].Tags.Tag | map(select( .TagKey | test("^(?!acs).*"))) | .[] | {"Key": .TagKey, "Value": .TagValue}'

Concatenate strings by using the specified character

FormatVersion: OOS-2019-06-01
Tasks:
- Name: describeInstances
  Action: ACS::ExecuteAPI
  Properties:
    Service: ECS
    API: DescribeInstances # API reference: https://www.alibabacloud.com/help/zh/ecs/developer-reference/api-ecs-2014-05-26-describeinstances
      Parameters:
        RegionId: cn-hangzhou
    Outputs:
      newInstanceName:
        Type: List # Filter the specified fields and concatenate the fields into a string by using the specified character.
        ValueSelector: '.Instances.Instance[]|[.InstanceName,.InstanceId,.EipAddress.AllocationId]|.[1],(.|join(":"))'

Merge multiple lists

FormatVersion: OOS-2019-06-01
Tasks:
- Name: describeInstances
  Action: ACS::ExecuteAPI
  Properties:
    Service: ECS
    API: DescribeInstances # API reference: https://www.alibabacloud.com/help/zh/ecs/developer-reference/api-ecs-2014-05-26-describeinstances
      Parameters:
        RegionId: cn-hangzhou
    Outputs:
      newInstanceName:
        Type: List # Filter the specified fields and merge them into a list for output.
        ValueSelector: '.Instances.Instance[] | .VpcAttributes.PrivateIpAddress.IpAddress + .InnerIpAddress.IpAddress + .PublicIpAddress.IpAddress'

Filter specific fields by comparing dates

FormatVersion: OOS-2019-06-01
Tasks:
  - Name: describeReservedInstances
    Action: ACS::ExecuteAPI
    Properties:
      Service: ECS
      API: DescribeReservedInstances # API reference: https://www.alibabacloud.com/help/zh/ecs/developer-reference/api-ecs-2014-05-26-describereservedinstances
      Parameters:
        RegionId: cn-hangzhou
    Outputs:
      reservedInstanceIds:
        Type: List # Filter the specified field based on the expiration time.
        ValueSelector: '.ReservedInstances.ReservedInstance[] | select(.ExpiredTime[0:11] == "{{ACS::CurrentDate}}") | .ReservedInstanceId'