0

I am writing a script that is meant to clean up Route53 records, so I want to add entries in an array to a JSON object as below

outer.json

{
  "Comment": "Delete record sets for $cluster_name",
  "Changes": [
    {
      "Action": "DELETE",
      "ResourceRecordSet": {}
    }
  ]
}

records.json

[
  {
    "Name": "abcde_svcx.$cluster_name.domain.com.",
    "Type": "TXT",
    "TTL": 300,
    "ResourceRecords": [
      {
        "Value": "\"heritage=external-dns,external-dns/owner=$cluster_name,external-dns/resource=service/svcx/svcx-cluster\""
      }
    ]
  },
  {
    "Name": "svcx.$cluster_name.domain.com.",
    "Type": "A",
    "AliasTarget": {
      "HostedZoneId": "123456789",
      "DNSName": "some-value.elb.us-east-2.amazonaws.com.",
      "EvaluateTargetHealth": true
    }
  }
]

I would like to have an output that looks like below:

output.json

{
  "Comment": "Delete record sets for $cluster_name",
  "Changes": [
    {
      "Action": "DELETE",
      "ResourceRecordSet": {
        "Name": "abcde_svcx.$cluster_name.domain.com.",
        "Type": "TXT",
        "TTL": 300,
        "ResourceRecords": [
          {
            "Value": "\"heritage=external-dns,external-dns/owner=$cluster_name,external-dns/resource=service/svcx/svcx-cluster\""
          }
        ]
      }
    },
    {
      "Action": "DELETE",
      "ResourceRecordSet": {
        "Name": "svcx.$cluster_name.domain.com.",
        "Type": "A",
        "AliasTarget": {
          "HostedZoneId": "123456789",
          "DNSName": "some-value.elb.us-east-2.amazonaws.com.",
          "EvaluateTargetHealth": true
        }
      }
    }
  ]
}

Is this something that can be done in a shell script using jq?

1 Answer 1

1

Update |= the .Changes array by adding to its first item's ResourceRecordSet field the second file's array items input[]:

jq '.Changes |= [first | .ResourceRecordSet += input[]]' outer.json records.json
{
  "Comment": "Delete record sets for $cluster_name",
  "Changes": [
    {
      "Action": "DELETE",
      "ResourceRecordSet": {
        "Name": "abcde_svcx.$cluster_name.domain.com.",
        "Type": "TXT",
        "TTL": 300,
        "ResourceRecords": [
          {
            "Value": "\"heritage=external-dns,external-dns/owner=$cluster_name,external-dns/resource=service/svcx/svcx-cluster\""
          }
        ]
      }
    },
    {
      "Action": "DELETE",
      "ResourceRecordSet": {
        "Name": "svcx.$cluster_name.domain.com.",
        "Type": "A",
        "AliasTarget": {
          "HostedZoneId": "123456789",
          "DNSName": "some-value.elb.us-east-2.amazonaws.com.",
          "EvaluateTargetHealth": true
        }
      }
    }
  ]
}

Demo

Note: If outer.json's ResourceRecordSet is always empty, you may as well just make an assignment = instead of an updating addition +=.

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.