Reputation: 5674
Having the following structure:
{
"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
}
]
}
}
}
}
I extracted the items and modified them using the following:
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
)')
Now I have the following stored in lambdaFunctionAssociations:
[
{
"LambdaFunctionARN": "ZZZ",
"EventType": "origin-response",
"IncludeBody": false
},
{
"LambdaFunctionARN": "YYY",
"EventType": "viewer-request",
"IncludeBody": false
},
{
"LambdaFunctionARN": "XXX",
"EventType": "origin-request",
"IncludeBody": false
}
]
I want to replace the "Items": in the org json trying this:
export updatedCloudFrontConf=$(echo $json | jq '.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations.Items='$lambdaFunctionAssociations'')
with thefollowing error:
jq: error: syntax error, unexpected $end (Unix shell quoting issues?) at <top-level>, line 1:
.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations.Items=[
Upvotes: 0
Views: 476
Reputation: 50750
Not to diminish your efforts at all, but that's not how we use jq. You can do this in a single call, e.g:
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
Upvotes: 3