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?
CodePudding user response:
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
}
}
}
]
}
Note: If outer.json
's ResourceRecordSet
is always empty, you may as well just make an assignment =
instead of an updating addition =
.