Home > database >  select and delete attributes with JQ
select and delete attributes with JQ

Time:03-11

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.

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

CodePudding user response:

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

  • Related