skip to Main Content

We have a scenario where we need to replace the TargetOriginID of CacheBehaviour in the distribution of a json file. We need to replace the Existing TargetOriginID with New Values. I have tried with below jq command but not getting any closer

for targetoriginID in $(jq '(.CacheBehaviors.Items[].TargetOriginId)' distconfig.json);

         do 
                  
                 
                 echo "#######fetch the new value from change behaviour json file"#######
                 NewValue=$(jq -r "map(select(.targetoriginid == ""$targetoriginID""))[].targetorigindr" changebehaviour.json)


echo "#########replace value in dist config json file with new value from change behaviour###########"
jq -r '(.CacheBehaviors.Items[].TargetOriginId | select(. == "$targetoriginID")) = "$NewValue"' distconfig.json > "tmp" && mv "tmp" distconfig.json
{

      "CachedMethods": {
        "Quantity": 3,
        "Items": [
          "HEAD",
          "GET",
          "OPTIONS"
        ]
      }
    },
    "SmoothStreaming": false,
    "Compress": false,
    "LambdaFunctionAssociations": {
      "Quantity": 0
    },
    "FunctionAssociations": {
      "Quantity": 0
    },
    "FieldLevelEncryptionId": "",
    "ForwardedValues": {
      "QueryString": true,
      "Cookies": {
        "Forward": "none"
      },
      "Headers": {
        "Quantity": 9,
        "Items": [
          "Authorization",
          "Origin",
          "access-control-allow-credentials",
          "expires",
          "access-control-max-age",
          "access-control-allow-headers",
          "cache-control",
          "access-control-allow-methods",
          "pragma"
        ]
      },
      "QueryStringCacheKeys": {
        "Quantity": 1,
        "Items": [
          "*"
        ]
      }
    },
    "MinTTL": 0,
    "DefaultTTL": 86400,
    "MaxTTL": 31536000
  },
  "CacheBehaviors": {
    "Quantity": 2,
    "Items": [
      {
        "PathPattern": "jkl/*",
        "TargetOriginId": "nkl/Prod",
        "TrustedSigners": {
          "Enabled": false,
          "Quantity": 0
        },
        "TrustedKeyGroups": {
          "Enabled": false,
          "Quantity": 0
        },
        "ViewerProtocolPolicy": "redirect-to-https",
        "AllowedMethods": {
          "Quantity": 7,
          "Items": [
            "HEAD",
            "DELETE",
            "POST",
            "GET",
            "OPTIONS",
            "PUT",
            "PATCH"
          ],
          "CachedMethods": {
            "Quantity": 3,
            "Items": [
              "HEAD",
              "GET",
              "OPTIONS"
            ]
          }
        },
        "SmoothStreaming": false,
        "Compress": false,
        "LambdaFunctionAssociations": {
          "Quantity": 0
        },
        "FunctionAssociations": {
          "Quantity": 0
        },
        "FieldLevelEncryptionId": "",
        "ForwardedValues": {
          "QueryString": true,
          "Cookies": {
            "Forward": "all"
          },
          "Headers": {
            "Quantity": 9,
            "Items": [
              "Authorization",
              "Origin",
              "access-control-allow-credentials",
              "access-control-max-age",
              "access-control-allow-headers",
              "cache-control",
              "access-control-allow-methods",
              "expirers",
              "pragma"
            ]
          },
          "QueryStringCacheKeys": {
            "Quantity": 1,
            "Items": [
              "*"
            ]
          }
        },
        "MinTTL": 0,
        "DefaultTTL": 86400,
        "MaxTTL": 31536000
      },
      {
        "PathPattern": "fgh/*",
        "TargetOriginId":"xyz/Prod",
        "TrustedSigners": {
          "Enabled": false,
          "Quantity": 0
        },
        "TrustedKeyGroups": {
          "Enabled": false,
          "Quantity": 0
        },
        "ViewerProtocolPolicy": "redirect-to-https",
        "AllowedMethods": {
          "Quantity": 7,
          "Items": [
            "HEAD",
            "DELETE",
            "POST",
            "GET",
            "OPTIONS",
            "PUT",
            "PATCH"
          ],
          "CachedMethods": {
            "Quantity": 3,
            "Items": [
              "HEAD",
              "GET",
              "OPTIONS"
            ]
          }
        },
        "SmoothStreaming": false,
        "Compress": false,
        "LambdaFunctionAssociations": {
          "Quantity": 0
        },
        "FunctionAssociations": {
          "Quantity": 0
        },
        "FieldLevelEncryptionId": "",
        "ForwardedValues": {
          "QueryString": true,
          "Cookies": {
            "Forward": "none"
          },
          "Headers": {
            "Quantity": 10,
            "Items": [
              "access-control-allow-origin",
              "authorization",
              "Origin",
              "access-control-allow-credentials",
              "access-control-max-age",
              "access-control-allow-headers",
              "cache-control",
              "access-control-allow-methods",
              "expirers",
              "pragma"
            ]
          },
          "QueryStringCacheKeys": {
            "Quantity": 1,
            "Items": [
              "*"
            ]
          }
        },
        "MinTTL": 0,
        "DefaultTTL": 0,
        "MaxTTL": 0
      }
    ]
  }

Looking for a solution to make bulk change in CacheBehaviour for all TargetOriginID’s

3

Answers


  1. As of this writing, the posted JSON is invalid, so it’s not clear exactly what is needed, but the following is illustrative:

    jq --arg newvalue xyz '
      (.. | objects | select(has("TargetOriginId")) | .TargetOriginId) |= $newvalue
    '
    
    Login or Signup to reply.
  2. Fixed (i.e. made valid) and reduced your JSON input down to the necessary minimum (the M in MRE), that is 50 lines.

    {
      "CacheBehaviors": {
        "Quantity": 2,
        "Items": [
          {
            "PathPattern": "jkl/*",
            "TargetOriginId": [
              "nkl"
            ],
            "TrustedSigners": {
              "Enabled": false,
              "Quantity": 0
            },
            "TrustedKeyGroups": {
              "Enabled": false,
              "Quantity": 0
            },
            "ViewerProtocolPolicy": "redirect-to-https"
          },
          {
            "PathPattern": "fgh/*",
            "TargetOriginId": [
              "xyz"
            ],
            "TrustedSigners": {
              "Enabled": false,
              "Quantity": 0
            },
            "TrustedKeyGroups": {
              "Enabled": false,
              "Quantity": 0
            },
            "ViewerProtocolPolicy": "redirect-to-https"
          }
        ]
      },
      "CustomErrorResponses": {
        "Quantity": 0
      },
      "Comment": "vvvv",
      "Logging": {
        "Enabled": true,
        "IncludeCookies": false,
        "Bucket": "abc.s3.amazonaws.com",
        "Prefix": "std"
      },
      "WebACLId": "",
      "HttpVersion": "http2",
      "IsIPV6Enabled": true
    }
    

    You probably want to run a jq program similar to the following:

    (.CacheBehaviors.Items[].TargetOriginId | select(. as $id | "nkl" | IN($id[]))) = ["wkl"]
    

    It selects all TargetOriginIds which contain the value "nkl" and changes the list to only contain "wkl".

    Output:

    {
      "CacheBehaviors": {
        "Quantity": 2,
        "Items": [
          {
            "PathPattern": "jkl/*",
            "TargetOriginId": [
              "wkl"
            ],
            "TrustedSigners": {
              "Enabled": false,
              "Quantity": 0
            },
            "TrustedKeyGroups": {
              "Enabled": false,
              "Quantity": 0
            },
            "ViewerProtocolPolicy": "redirect-to-https"
          },
          {
            "PathPattern": "fgh/*",
            "TargetOriginId": [
              "xyz"
            ],
            "TrustedSigners": {
              "Enabled": false,
              "Quantity": 0
            },
            "TrustedKeyGroups": {
              "Enabled": false,
              "Quantity": 0
            },
            "ViewerProtocolPolicy": "redirect-to-https"
          }
        ]
      },
      "CustomErrorResponses": {
        "Quantity": 0
      },
      "Comment": "vvvv",
      "Logging": {
        "Enabled": true,
        "IncludeCookies": false,
        "Bucket": "abc.s3.amazonaws.com",
        "Prefix": "std"
      },
      "WebACLId": "",
      "HttpVersion": "http2",
      "IsIPV6Enabled": true
    }
    

    From the question it is unclear if "TargetOriginId": ["nkl", "xyz"] should become ["wkl"] or ["wkl", "xyz"]. Or whether ["nkl", "xyz"] is a match because it contains different values too. Maybe you only want to select those where the full TargetOriginId array matches?

    If you only want to match items with single-valued target origin ids, the program becomes a bit simpler:

    (.CacheBehaviors.Items[].TargetOriginId | select(. == ["nkl"])) = ["wkl"]
    

    If your target origin ids are always an array and you want to change say "[abc,nkl,xyz]" to "[abc,REPLACED,xyz]", then select only those array elements and assign them the new value.

    New input (as I understood it, the Q is quite vague on this):

    {
      "CacheBehaviors": {
        "Quantity": 2,
        "Items": [
          {
            "PathPattern": "jkl/*",
            "TargetOriginId": [
              "nkl",
              "something else"
            ]
          },
          {
            "PathPattern": "fgh/*",
            "TargetOriginId": [
              "xyz",
              "abc"
            ]
          }
        ]
      },
      "IsIPV6Enabled": true
    }
    

    jq:

    (.CacheBehaviors.Items[].TargetOriginId[] | select(. == "nkl")) = "wkl"
    

    Output:

    {
      "CacheBehaviors": {
        "Quantity": 2,
        "Items": [
          {
            "PathPattern": "jkl/*",
            "TargetOriginId": [
              "wkl",
              "something else"
            ]
          },
          {
            "PathPattern": "fgh/*",
            "TargetOriginId": [
              "xyz",
              "abc"
            ]
          }
        ]
      },
      "IsIPV6Enabled": true
    }
    
    Login or Signup to reply.
  3. Providing a second answer with some assumption. Let’s start with a minimal example input:

    {
      "CacheBehaviors": {
        "Quantity": 2,
        "Items": [
          {
            "PathPattern": "jkl/*",
            "TargetOriginId": [
              "nkl",
              "something else"
            ]
          },
          {
            "PathPattern": "fgh/*",
            "TargetOriginId": [
              "xyz",
              "abc"
            ]
          }
        ]
      },
      "IsIPV6Enabled": true
    }
    

    Assuming you want to define a mapping "{old1: new1, old2: new2, old3: new3, …}", the following program could work and is easily extendable:

    (.CacheBehaviors.Items[].TargetOriginId[]) |= ({
      nkl: "wkl",
      xyz: "123",
      "something else": "is changed too",
      "not found": "never replaced"
    }[.] // .)
    

    And if you like to have your mapping at the top/start of your program, bind it to a variable:

    {
      nkl: "wkl",
      xyz: "123",
      "something else": "is changed too",
      "not found": "rever replaced"
    } as $mapping
    | (.CacheBehaviors.Items[].TargetOriginId[]) |= ($mapping[.] // .)
    

    Output after running through above’s program:

    {
      "CacheBehaviors": {
        "Quantity": 2,
        "Items": [
          {
            "PathPattern": "jkl/*",
            "TargetOriginId": [
              "wkl",
              "is changed too"
            ]
          },
          {
            "PathPattern": "fgh/*",
            "TargetOriginId": [
              "123",
              "abc"
            ]
          }
        ]
      },
      "IsIPV6Enabled": true
    }
    

    Is that what you are after? This chooses the new value by key from an object/dictionary/map and falls back to the current value if no mapping was found.

    … |= ({ old: "new" }[.] // .)
    

    The mapping itself could be provided as an argument to jq from a separate file:

    jq --slurpfile mapping old_to_new_ids.json 
      '(.CacheBehaviors.Items[].TargetOriginId[]) |= ($mapping[0][.] // .)'
    

    With the contents of file old_to_new_ids.json simply being:

    {
      "nkl": "wkl",
      "xyz": "123",
      "something else": "is changed too",
      "not found": "never replaced"
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search