gpt4 book ai didi

bash - 在 BASH 中使用 jq 将数组附加到 json

转载 作者:行者123 更新时间:2023-12-04 19:01:24 27 4
gpt4 key购买 nike

我有一个看起来像这样的 json:

{
"failedSet": [],
"successfulSet": [{
"event": {
"arn": "arn:aws:health:us-east-1::event/AWS_RDS_MAINTENANCE_SCHEDULED_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx",
"endTime": 1502841540.0,
"eventTypeCategory": "scheduledChange",
"eventTypeCode": "AWS_RDS_MAINTENANCE_SCHEDULED",
"lastUpdatedTime": 1501208541.93,
"region": "us-east-1",
"service": "RDS",
"startTime": 1502236800.0,
"statusCode": "open"
},
"eventDescription": {
"latestDescription": "We are contacting you to inform you that one or more of your Amazon RDS DB instances is scheduled to receive system upgrades during your maintenance window between August 8 5:00 PM and August 15 4:59 PM PDT. Please see the affected resource tab for a list of these resources. \r\n\r\nWhile the system upgrades are in progress, Single-AZ deployments will be unavailable for a few minutes during your maintenance window. Multi-AZ deployments will be unavailable for the amount of time it takes a failover to complete, usually about 60 seconds, also in your maintenance window. \r\n\r\nPlease ensure the maintenance windows for your affected instances are set appropriately to minimize the impact of these system upgrades. \r\n\r\nIf you have any questions or concerns, contact the AWS Support Team. The team is available on the community forums and by contacting AWS Premium Support. \r\n\r\nhttp://aws.amazon.com/support\r\n"
}
}]
}

我正在尝试在 successfulSet[].event 下添加新的键/值(键名为 affectedEntities )使用 jq,我看过一些示例,例如 herehere ,但这些答案都没有真正显示如何添加一个可能的具有多个值的键(我之所以说可能是因为到目前为止,AWS 正在为受影响的实体返回一个值,但如果还有更多,那么我我想列出它们)。

编辑:我要添加的新键的值存储在一个名为 $affected_entities 的变量中该值的示例如下所示:
[
"arn:aws:acm:us-east-1:xxxxxxxxxxxxxx:certificate/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"
]

该值可能如下所示:
[
"arn:aws:acm:us-east-1:xxxxxxxxxxxxxx:certificate/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx",
"arn:aws:acm:us-east-1:xxxxxxxxxxxxxx:certificate/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx",
...
...
...
]

最佳答案

你可以用这个jq ,

jq '.successfulSet[].event += { "new_key" : "new_value" }' file.json

编辑:

尝试这个:
jq --argjson argval "$new_value" '.successfulSet[].event += { "affected_entities" : $argval }' file.json

测试:
sat~$ new_value='[
"arn:aws:acm:us-east-1:xxxxxxxxxxxxxx:certificate/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"
]'

sat~$ jq --argjson argval "$new_value" '.successfulSet[].event += { "affected_entities" : $argval }' file.json

请注意 --argjson适用于 jq 1.5 及更高版本。

关于bash - 在 BASH 中使用 jq 将数组附加到 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45628947/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com