30

Is there any way / class / module in python to compare two json objects and print the changes/differences?

I have tried with "json_tools" which is gives fairly good results, however diff failed in case if there are python lists' with elements in different orders in two json objects.

e.g.

JSON 1:

{
    'Person' : 
        {
            'FName'    : 'John',
            'LName'    : 'Rambo',
            'Sex'      : 'Male'
            'Height'   : '6 ft',
            'Weight'   : '90 KG',
            'Children' :
                [
                    {
                        'FName'  : 'Anna',
                        'LName'  : 'Rambo',
                        'Sex'    : 'Female',
                        'Height' : '5 ft',
                        'Weight' : '55 KG',
                    },
                    {
                        'FName'  : 'Jemmy',
                        'LName'  : 'Rambo',
                        'Sex'    : 'Male',
                        'Height' : '5 ft',
                        'Weight' : '60 KG',
                    }

                ]
        }
}

JSON 2:

{
    'Person' : 
        {
            'FName'    : 'John',
            'LName'    : 'Rambo',
            'Sex'      : 'Male'
            'Height'   : '6 ft',
            'Weight'   : '90 KG',
            'Children' :
                [
                    {
                        'FName'  : 'Jemmy',
                        'LName'  : 'Rambo',
                        'Sex'    : 'Male',
                        'Height' : '5 ft',
                        'Weight' : '60 KG',
                    },
                    {
                        'FName'  : 'Anna',
                        'LName'  : 'Rambo',
                        'Sex'    : 'Female',
                        'Height' : '5 ft',
                        'Weight' : '55 KG',
                    }
                ]
        }
}

json diff shows the Two jsons are mismatched.. Logically those are identical..

Is there a good way of json matching and comparing in python?

martineau
  • 119,623
  • 25
  • 170
  • 301
Abhishek Kulkarni
  • 3,693
  • 8
  • 35
  • 42

4 Answers4

25

You can use jsondiff

from jsondiff import diff
diff(json1, json2)

... assuming you have json1 and json2 loaded with the json entries from your example (and by the way, you have a missing comma after the 'sex' entry).

user2111922
  • 891
  • 9
  • 9
  • 1
    Great answer! Helped me compare two auto-generated json files and work the differences until the `diff` output was empty, just like I wanted. – Capt. Crunch Jun 17 '19 at 05:18
  • I found https://stackoverflow.com/questions/25851183/how-to-compare-two-json-objects-with-the-same-elements-in-a-different-order-equa/25851972 this link more helpful – Akhilraj N S Oct 21 '20 at 07:05
  • does this work on nested array of json objects as well? – sattva_venu Jan 28 '21 at 12:11
16

you can use deepdiff with ignore_order=True

from deepdiff import DeepDiff
t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3]}}
t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 3, 2, 3]}}
ddiff = DeepDiff(t1, t2, ignore_order=True)
print (ddiff)
{}
Nir Soudry
  • 379
  • 3
  • 6
9

Logically those are identical.

They're not. Order matters in a JSON array. I don't know of any tools that will ignore order for you. You could try recursing over the deserialized structure, turning lists into some sort of multiset and dicts into some sort of hashable, frozen dict (so you can put them into multisets), then running your own diff routine on that.

user2357112
  • 260,549
  • 28
  • 431
  • 505
3

You could try doing a diff on the results of json.dumps(jobj, sort_keys=True)

petezurich
  • 9,280
  • 9
  • 43
  • 57
Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
  • 1
    `sort_keys` won't reorder JSON arrays; it only affects the order in which JSON object key-value pairs are serialized. – user2357112 Dec 01 '17 at 17:22