If you want two objects with the same elements but in a different order to compare equal, then the obvious thing to do is compare sorted copies of them - for instance, for the dictionaries represented by your JSON strings a and b:
import json
a = json.loads("""
{
"errors": [
{"error": "invalid", "field": "email"},
{"error": "required", "field": "name"}
],
"success": false
}
""")
b = json.loads("""
{
"success": false,
"errors": [
{"error": "required", "field": "name"},
{"error": "invalid", "field": "email"}
]
}
""")
>>> sorted(a.items()) == sorted(b.items())
False
... but that doesn't work, because in each case, the "errors" item of the top-level dict is a list with the same elements in a different order, and sorted() doesn't try to sort anything except the "top" level of an iterable.
To fix that, we can define an ordered function which will recursively sort any lists it finds (and convert dictionaries to lists of (key, value) pairs so that they're orderable):
def ordered(obj):
if isinstance(obj, dict):
return sorted((k, ordered(v)) for k, v in obj.items())
if isinstance(obj, list):
return sorted(ordered(x) for x in obj)
else:
return obj
If we apply this function to a and b, the results compare equal:
>>> ordered(a) == ordered(b)
True
Answer from Zero Piraeus on Stack OverflowIf you want two objects with the same elements but in a different order to compare equal, then the obvious thing to do is compare sorted copies of them - for instance, for the dictionaries represented by your JSON strings a and b:
import json
a = json.loads("""
{
"errors": [
{"error": "invalid", "field": "email"},
{"error": "required", "field": "name"}
],
"success": false
}
""")
b = json.loads("""
{
"success": false,
"errors": [
{"error": "required", "field": "name"},
{"error": "invalid", "field": "email"}
]
}
""")
>>> sorted(a.items()) == sorted(b.items())
False
... but that doesn't work, because in each case, the "errors" item of the top-level dict is a list with the same elements in a different order, and sorted() doesn't try to sort anything except the "top" level of an iterable.
To fix that, we can define an ordered function which will recursively sort any lists it finds (and convert dictionaries to lists of (key, value) pairs so that they're orderable):
def ordered(obj):
if isinstance(obj, dict):
return sorted((k, ordered(v)) for k, v in obj.items())
if isinstance(obj, list):
return sorted(ordered(x) for x in obj)
else:
return obj
If we apply this function to a and b, the results compare equal:
>>> ordered(a) == ordered(b)
True
Another way could be to use json.dumps(X, sort_keys=True) option:
import json
a, b = json.dumps(a, sort_keys=True), json.dumps(b, sort_keys=True)
a == b # a normal string comparison
This works for nested dictionaries and lists.
A good method for finding any differences between two json files?
python - Comparison of JSON file key values - Stack Overflow
Diff two large JSON array or objects
JSON and Dictionary
Videos
If you want to print only the difference in subjson (and not the whole structure from the root), you may want to use recursive request
def json_compare(json1, json2):
#Compare all keys
for key in json1.keys():
#if key exist in json2:
if key in json2.keys():
#If subjson
if type(json1[key]) == dict:
json_compare(json1[key], json2[key])
else:
if json1[key] != json2[key]:
print "These entries are different:"
print json1[key]
print json2[key]
else:
print "found new key in json1 %r" % key
return True
you can do something like this,
import json
a = json.loads(json_1) #your json
b = json.loads(json_2) #json you want to compare
#iterating through all keys in b
for key in b.keys():
value = b[key]
if key not in a:
print "found new key {0} with value {1}".format(key, value)
else:
#check if values are not same
if a[key] != value: print "for key %s values are different" % key
ยป pip install json-diff
ยป pip install jsoncomparison
ยป pip install json-files-compare
I have a Python lambda function downloading a large excel file and converting it to JSON.
This file will be downloaded at least once a day (as the data can change)
I need to push the changed/updated data to an API.
Is there a way for me to compare two JSON files and output the diff?
It would be perfect if it would output multiple arrays of objects.
1 array of objects that have changed (I donโt care what has changed, just need to know that it has)
1 array of removed/deleted objects.