Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

select and delete attributes with JQ

Info

I have a terraform state file (json) with some deprecated attributes.

I would like to remove theses deprecated attributes.

I try to use jq and select() && del() but did not succeed to get back my full json without the deprecated attribue timeouts.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Problem

How to get my full json without the attribute timeouts for only one type of resources google_dns_record_set.

Data

{
  "version": 4,
  "terraform_version": "1.0.6",
  "serial": 635,
  "lineage": "6a9c2392-fdae-2b54-adcc-7366f262ffa4",
  "outputs": {"test":"test1"},
  "resources": [
  {
      "module": "module.resources",
      "mode": "data",
      "type": "google_client_config"
  },
  {
      "module": "module.xxx.module.module1[\"cluster\"]",
      "mode": "managed",
      "type": "google_dns_record_set",
      "name": "public_ip_ic_dns",
      "provider": "module.xxx.provider[\"registry.terraform.io/hashicorp/google\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "id": "projects/xxx-xxx/managedZones/xxx--public/rrsets/*.net1.cluster.xxx--public.net.com./A",
            "managed_zone": "xxx--public",
            "name": "*.net1.cluster.xxx--public.net.com.",
            "project": "xxx-xxx",
            "rrdatas": [
              "11.22.33.44"
            ],
            "timeouts": null,
            "ttl": 300,
            "type": "A"
          },
          "sensitive_attributes": [],
          "private": "xxx",
          "dependencies": [
            "xxx"
          ]
        }
      ]
    }
  ]
}

Command

jq -r '.resources[] | select(.type=="google_dns_record_set").instances[].attributes | del(.timeouts)' data.json

>Solution :

Pull the del command up front to include the whole selection as its own filter

del(.resources[] | select(.type=="google_dns_record_set").instances[].attributes.timeouts)

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading