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文檔:http://bestwisewords.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文檔:http://bestwisewords.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文檔:http://bestwisewords.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文檔:http://bestwisewords.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文檔:http://bestwisewords.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文檔:http://bestwisewords.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'
文檔內容是否對您有幫助?