0

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=[
0

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.