Home > Software engineering >  Compare two json file and generate custom output on comparison
Compare two json file and generate custom output on comparison

Time:09-16

Need a hand on how to compare two technically same json files and output the differences between them.

I made some research on how to approach the problem but none of them quite close that what I want to achive here;

  • For certain key's values (["id", "dday", "cake_name"]) label them as 'matched' in the output.

  • For certain key's values (["Mainmetrics", "TC", "LSS","LTC"]) numerically check them and label them as percent differences if the numbers are not exactly the same.

  • Bonus: output the number of lines in each file.

    #Reference file

     {
     "Cake_make": {
     "id": "25803",
     "dday": "2009-01-01T15:00:00",
     "cake_name": "chocolate",
     "Mainmetrics": "7.6",
     "TC": "10000.02",
     "LSS":"102341.32",
     "LTC":"12321.65",  
     "anticipations": [
        {
         "time": "2009-01-01T04:00:00",
         "cake_name": "1",
         "top_properties": {
           "LT:TB2341": "0.23",
           "LS:TB2342": "63.23",
           "LTC:TB2343": "1"
         }
         },
         {
         "time": "2009-01-01T23:00:00",
         "cake_name": "1",
         "top_properties": {
           "LT:TB2341": "0.23",
           "LS:TB2342": "63.23",
           "LTC:TB2343": "37"
          }
       }
     ]
    

    } }

#Sample file to make comparison to reference

sample1 =  {
"Cake_make": {
    "id": "25803",
    "dday": "2009-01-01T15:00:00",
    "cake_name": "banana",
    "Mainmetrics": "15.2",
    "TC": "7362",
    "LSS":"97234.32",
    "LTC":"6566.65",  
  
    "anticipations": [
      {
        "time": "2009-01-01T15:00:00",
        "cake_name": "1",
        "top_properties": {
          "LT:TB2341": "0.23",
          "LS:TB2342": "63.23",
          "LTC:TB2343": "37"
        }
        }
    ]
}
} 



import json
import os

os.chdir(os.path.dirname(os.path.realpath(__file__)))
with open("reference.json", "r") as ref ,  open("sample1.json", "r") as samp:
    ref = json.loads(ref.read())
    samp = json.loads(samp.read())

reflength = len(ref)
samplelength = len(samp)
#print(reflength, samplelength)

exact_match_keys = ["id", "dday", "cake_name"]
percent_match_keys = ["Mainmetrics", "TC", "LSS","LTC"]

The expected output that I'm looking for is

enter image description here

So if anyone got any idea how to achieve this, then please share it and if need more clarifications then plzz let me know.

Is there a good, controllable way to compare two .json files and generate their difference into an Excel Sheet in Python

How to compare two json file and print report of differences

compare two json files and match entries with the same value

How to compare two json objects to check what has changed?

CodePudding user response:

diff = {}

for k, v in ref.items():
    diff[k] = {}
    for key, ref_value in v.items():
        if key in exact_match_keys:
            if ref_value == samp[k][key]:
                diff[k][key] = "matched"
            else:
                diff[k][key] = "Not Matched"
        elif key in percent_match_keys:
            ref_n, samp_n = float(ref_value), float(samp[k][key])
            p_dif = abs(ref_n - samp_n) / ref_n * 100
            diff[k][key] = f"%{p_dif} difference"
  • Related