I have a BASH Array as follows:
TEMPARRAY=( "1 A" "2 B" )
I want this array to convert to JSON Array (or Key Value Pair ?), like this:
{
"Comment": "MX Record for XYZ",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "XYZ",
"Type": "MX",
"TTL": 300,
"ResourceRecords": [
{
"Value": "1 A"
},
{
"Value": "2 B"
}
]
}
}
]
}
USE CASE: I am creating a shell script to add AWS Route53 DNS Records and I am stuck at specifying multiple values for MX records. If I update the MX records, it get replaced with a newer one.
Sample Code from my script:
if [[ "$MXCOUNT" -gt "1" ]]; then
TEMPARRAY=( "$(grep -i MX "$DNSFILE" | cut -d, -f3)" )
for i in "${TEMPARRAY[@]}"; do
# POSSIBLE CODE HERE
done
else
addMXrecord "$DNSNAME" "$DNSVALUE"
fi
The function addMXrecord will be containing the JSON (although it's for a single MX record.)
DNSFILE is in format:
DOMAIN,MX,1 A
DOMAIN,MX,2 B
Happy to provide anymore information.
jq -cRn '[inputs]' < <( (( ${#array[@]} )) && printf '%s\n' "${array[@]}")