具有以下结构:
{
"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
}
]
我想在组织json中替换“ Items”:
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=[
答案 0 :(得分:2)
根本不减少您的工作量,但这不是我们使用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