Home > other >  Diff two files in different formats TXT/JSON
Diff two files in different formats TXT/JSON

Time:03-16

I have two files which contain similar data but in different formats (JSON/TXT).

file-A.json

        {
        "Name": "www.mydomain.com.",
        "Type": "A",
        "TTL": 600,
        "ResourceRecords": [
            {
                "Value": "8.8.8.8"
            }
        ]
    },
    {
        "Name": "other.domain.com.",
        "Type": "NS",
        "TTL": 1800,
        "ResourceRecords": [
            {
                "Value": "123.reg."
            }
        ]
    }
]

}

file-B.txt

www.mydomain.com 600 IN A 8.8.8.8
other.domain.com 1800 IN NS 123.reg.

I'm trying to figure out the best method to diff both files so i can see if those records exist/match. However, as they are in different formats i'm not sure this is possible, so my idea was to parse both files to have an identical format then run diff on them but i don't know how to even start.

I've been able to parse the json using jq to obtain specific records

cat file-B.json | jq '.ResourceRecordSets[] | {"Name": .Name,"ResourceRecords": .[]}' 

but starting to think this is not even the best approach.

CodePudding user response:

The main approach is to get both data in the same format.

So lets convert the JSON file to the same format as that of the text file with the help of jq:

jq -r '
# For all array entries
.[] | 
  # Concatenates values to compose a flat DNS zone text file
  .Name  
  " "  
  (.TTL|tostring)  
  " IN "  
  .Type  
  " "  
  .ResourceRecords[0].Value
' \
file-A.json |
  diff - file-B.txt
  • Related