[英]how to replace entire array using jq in shell script?
具有以下结构:
{
"DistributionConfig": {
"DefaultCacheBehavior": {
"LambdaFunctionAssociations": {
"Quantity": 3,
"Items": [
{
"LambdaFunctionARN": "3",
"EventType": "origin-response",
"IncludeBody": false
},
{
"LambdaFunctionARN": "2",
"EventType": "viewer-request",
"IncludeBody": false
},
{
"LambdaFunctionARN": "1",
"EventType": "origin-request",
"IncludeBody": false
}
]
}
}
}
}
我提取了项目并使用以下内容对其进行了修改:
export lambdaFunctionAssociations=$(echo $json | jq '.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations.Items' |
jq 'map(if .EventType == "origin-response"
then . + {"LambdaFunctionARN":'$originResponse'}
else .
end
)' |
jq 'map(if .EventType == "viewer-request"
then . + {"LambdaFunctionARN":'$viewerRequest'}
else .
end
)' |
jq 'map(if .EventType == "origin-request"
then . + {"LambdaFunctionARN":'$originRequest'}
else .
end
)')
现在我在 lambdaFunctionAssociations 中存储了以下内容:
[
{
"LambdaFunctionARN": "ZZZ",
"EventType": "origin-response",
"IncludeBody": false
},
{
"LambdaFunctionARN": "YYY",
"EventType": "viewer-request",
"IncludeBody": false
},
{
"LambdaFunctionARN": "XXX",
"EventType": "origin-request",
"IncludeBody": false
}
]
我想替换 "Items": 在 org json 中尝试这个:
export updatedCloudFrontConf=$(echo $json | jq '.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations.Items='$lambdaFunctionAssociations'')
有以下错误:
jq: error: syntax error, unexpected $end (Unix shell quoting issues?) at <top-level>, line 1:
.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations.Items=[
根本不会减少您的努力,但这不是我们使用 jq 的方式。 您可以在一次调用中完成此操作,例如:
jq --arg originResponse ZZZ --arg viewerRequest YYY --arg originRequest XXX '
.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations.Items |= map(
.EventType as $t | .LambdaFunctionARN =
if $t == "origin-response" then $originResponse
elif $t == "viewer-request" then $viewerRequest
elif $t == "origin-request" then $originRequest
else . end
)' file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.