我有两个json文件。
我想将targetgroup.json和autoscaling.json中的对象与bash shell中的TargetGroupARNs和TargetGroupArn唯一值合并。
由于某些原因,我正在使用jq 1.4版,并且无法升级到最新版本。
#cat autoscaling.json
{
"AutoScalingGroups": [
{
"AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-1:050073205187:autoScalingGroup:29f22791-b9cf-49c2-9ba6-b9ac2759d767:autoScalingGroupName/asg-dev-srv-01-20190327_133322",
"ServiceLinkedRoleARN": "arn:aws:iam::050073205187:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling",
"TargetGroupARNs": [
"arn:aws:elasticloadbalancing:ap-northeast-1:050073205187:targetgroup/dev-trial-tg/ef79ad5c9df9014e"
]
},
{
"AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-1:050073205187:autoScalingGroup:cf662dc3-4e27-4022-a165-ebba8ca9488b:autoScalingGroupName/asg_group",
"ServiceLinkedRoleARN": "arn:aws:iam::050073205187:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling",
"TargetGroupARNs": [
"arn:aws:elasticloadbalancing:ap-northeast-1:050073205187:targetgroup/dev-trial1-tg/580ab71538a59063"
]
}
]
}
#cat targetgroup.json
[
{
"TargetGroupArn": "arn:aws:elasticloadbalancing:ap-northeast-1:050073205187:targetgroup/dev-trial-tg/ef79ad5c9df9014e",
"TargetGroupName": "dev-trial-tg"
},
{
"TargetGroupArn": "arn:aws:elasticloadbalancing:ap-northeast-1:050073205187:targetgroup/dev-trial1-tg/580ab71538a59063",
"TargetGroupName": "dev-trial1-tg"
}
]
我想要的是:
[
{
"AutoScalingGroups": [
{
"AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-1:050073205187:autoScalingGroup:29f22791-b9cf-49c2-9ba6-b9ac2759d767:autoScalingGroupName/asg-dev-srv-01-20190327_133322",
"ServiceLinkedRoleARN": "arn:aws:iam::050073205187:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling",
"TargetGroupARNs": [
"TargetGroupArn": "arn:aws:elasticloadbalancing:ap-northeast-1:050073205187:targetgroup/dev-trial-tg/ef79ad5c9df9014e",
"TargetGroupName": "dev-trial-tg"
],
},
{
"AutoScalingGroupARN": "arn:aws:autoscaling:ap-northeast-1:050073205187:autoScalingGroup:cf662dc3-4e27-4022-a165-ebba8ca9488b:autoScalingGroupName/asg_group",
"ServiceLinkedRoleARN": "arn:aws:iam::050073205187:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling",
"TargetGroupARNs": [
"TargetGroupArn": "arn:aws:elasticloadbalancing:ap-northeast-1:050073205187:targetgroup/dev-trial1-tg/580ab71538a59063",
"TargetGroupName": "dev-trial1-tg"
]
}
]
}
]
我尝试过,但是对我来说太难了。我希望你能帮助我。
答案 0 :(得分:0)
这是来自@oguzismail的解决方案
jq -n 'input | reduce (input|to_entries)[] as $p (.; .AutoScalingGroups[$p.key].TargetGroupARNs = $p.value)' autoscaling.json targetgroup.json