jq icon indicating copy to clipboard operation
jq copied to clipboard

Using JQ to delete blocks from json CloudFormation templates

Open Mathias-dcm opened this issue 3 years ago • 2 comments

I'm using jq (on bash) to delete key blocks from json templates. I'm currently deleting 50% of what I want, by using the "Type" element from CloudFormation templates :

jq '(del(.Resources | .[] | select(.Type=="AWS::CodePipeline::Pipeline")))' $fileName > newTemplate1.json

I Would also need to delete every resources that contains "cdkpipelinePipeline", before the Type block :

"developcdkpipelinePipelineRole": {
  "Type": "AWS::IAM::Role",
  "Properties": {
    "AssumeRolePolicyDocument": {
      "Statement": [
        {
          "Action": "sts:AssumeRole",
          "Effect": "Allow",
          "Principal": {
            "Service": "codepipeline.amazonaws.com"
          }
        }
      ],
      "Version": "2012-10-17"
    },
    "Tags": [
      {
        "Key": "Application",
        "Value": "develop"
      }
    ]
  },
  "DependsOn": [
    "AuroraNestedStackAuroraNestedStackResource"
  ],
  "Metadata": {
    "aws:cdk:path": "develop-cicd-stack/develop-cdkpipeline/Pipeline/Role/Resource"
  }
}

}, ... ``

Do you have any clue on how to achieve that ? I Basically want every blocks with a key that contains "cdkpipelinePipeline" to be removed from my template.

Thank you already

Mathias-dcm avatar Jun 08 '22 09:06 Mathias-dcm

How about jq 'delpaths([paths | select(contains(["cdkpipelinePipeline"]))])' ?

itchyny avatar Jun 08 '22 09:06 itchyny

Works just fine, take you very much :)

Mathias-dcm avatar Jun 08 '22 11:06 Mathias-dcm