jq
Updated at:
Overview
jq is a CLI tool for filtering, transforming, and processing JSON data. In CloudOps Orchestration Service (OOS) templates, you can use jq expressions in the ValueSelector parameter of Outputs to parse task output. For more information about the syntax, see Tasks.
jq documentation: jq Manual
jq online playground: jqplay
Basic usage
Filter specific fields
FormatVersion: OOS-2019-06-01
Tasks:
- Name: describeInstances
Action: ACS::ExecuteAPI
Properties:
Service: ECS
API: DescribeInstances # API reference: https://help.aliyun.com/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://help.aliyun.com/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://help.aliyun.com/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://help.aliyun.com/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://help.aliyun.com/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://help.aliyun.com/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'
Is this page helpful?