JQ简介
JQ 是一个强大的命令行工具,用于处理 JSON 数据。它允许用户以简洁、高效的方式过滤、转换和处理 JSON 文件或输入流中的数据。JQ 以其易读的语法和强大的功能,在处理 JSON 数据时特别有用。本文将介绍在模板中如何通过Outputs的ValueSelector解析任务输出,相关语法请查看任务说明文档。
JQ官网:https://jqlang.github.io/jq/manual/
JQ在线测试网站:https://jqplay.org/
基础用法
通过 JQ 过滤指定字段
FormatVersion: OOS-2019-06-01
Tasks:
- Name: describeInstances
Action: ACS::ExecuteAPI
Properties:
Service: ECS
API: DescribeInstances #API文档:https://help.aliyun.com/zh/ecs/developer-reference/api-ecs-2014-05-26-describeinstances
Parameters:
RegionId: cn-hangzhou
Outputs:
instanceIds:
Type: List # 过滤出所有实例ID
ValueSelector: Instances.Instance[].InstanceId
instanceId:
Type: String # 过滤出单个实例ID
ValueSelector: Instances.Instance[].InstanceId
instanceNames:
Type: List # 过滤出所有实例名称
ValueSelector: Instances.Instance[].InstanceName
instanceIdByCpu:
Type: String # 过滤出所有实例ID并按Cpu排序
ValueSelector: .Instances.Instance | sort_by(.Cpu) |.[].InstanceId
instanceIdByCreationTime:
Type: String # 过滤出所有实例ID并按CreationTime排序
ValueSelector: .Instances.Instance | sort_by(.CreationTime) |.[].InstanceId
通过 JQ 过滤指定格式的 JSON
- Name: queryDisks
Action: 'ACS::ExecuteAPI'
Properties:
Service: ECS
API: DescribeDisks #API文档:https://help.aliyun.com/zh/ecs/developer-reference/api-ecs-2014-05-26-describedisks
Parameters:
RegionId: cn-hangzhou
Outputs:
dataDisks:
Type: Json #过滤出数据盘信息
ValueSelector: '.Disks.Disk|map( select(.Type == "data")|{"Category":(.Category),"Device": (.Device),"Size":(.Size),"DiskName":(.DiskName),"PerformanceLevel":(.PerformanceLevel)})'
systemDisk:
Type: Json #过滤出系统盘信息
ValueSelector: '.Disks.Disk|map( select(.Type == "system")|{"Category":(.Category),"Device": (.Device),"Size":(.Size),"DiskName":(.DiskName),"PerformanceLevel":(.PerformanceLevel)})'
进阶用法
替换JSON中的Key
FormatVersion: OOS-2019-06-01
Tasks:
- Name: describeInstances
Action: ACS::ExecuteAPI
Properties:
Service: ECS
API: DescribeInstances #API文档:https://help.aliyun.com/zh/ecs/developer-reference/api-ecs-2014-05-26-describeinstances
Parameters:
RegionId: cn-hangzhou
Outputs:
tags:
Type: List # 过滤出非 acs 开头的 Tags 并把 TagKey 替换为 Key
ValueSelector: '.Instances.Instance[].Tags.Tag | map(select( .TagKey | test("^(?!acs).*"))) | .[] | {"Key": .TagKey, "Value": .TagValue}'
按指定字符拼接字符串
FormatVersion: OOS-2019-06-01
Tasks:
- Name: describeInstances
Action: ACS::ExecuteAPI
Properties:
Service: ECS
API: DescribeInstances #API文档:https://help.aliyun.com/zh/ecs/developer-reference/api-ecs-2014-05-26-describeinstances
Parameters:
RegionId: cn-hangzhou
Outputs:
newInstanceName:
Type: List # 过滤出目标字段并用指定字符串拼接
ValueSelector: '.Instances.Instance[]|[.InstanceName,.InstanceId,.EipAddress.AllocationId]|.[1],(.|join(":"))'
多个列表合并
FormatVersion: OOS-2019-06-01
Tasks:
- Name: describeInstances
Action: ACS::ExecuteAPI
Properties:
Service: ECS
API: DescribeInstances #API文档:https://help.aliyun.com/zh/ecs/developer-reference/api-ecs-2014-05-26-describeinstances
Parameters:
RegionId: cn-hangzhou
Outputs:
newInstanceName:
Type: List # 过滤出目标字段并合并到同一个List输出
ValueSelector: '.Instances.Instance[] | .VpcAttributes.PrivateIpAddress.IpAddress + .InnerIpAddress.IpAddress + .PublicIpAddress.IpAddress'
通过比较日期过滤出目标字段
FormatVersion: OOS-2019-06-01
Tasks:
- Name: describeReservedInstances
Action: ACS::ExecuteAPI
Properties:
Service: ECS
API: DescribeReservedInstances # API文档:https://help.aliyun.com/zh/ecs/developer-reference/api-ecs-2014-05-26-describereservedinstances
Parameters:
RegionId: cn-hangzho
Outputs:
reservedInstanceIds:
Type: List #通过过期时间过滤出目标字段
ValueSelector: '.ReservedInstances.ReservedInstance[] | select(.ExpiredTime[0:11] == "{{ACS::CurrentDate}}") | .ReservedInstanceId'
文档内容是否对您有帮助?