How to compare two JSON objects with the same elements in a different order equal? How to compare two JSON objects with the same elements in a different order equal? python python

How to compare two JSON objects with the same elements in a different order equal?


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 jsona = 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 jsona, 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.


Decode them and compare them as mgilson comment.

Order does not matter for dictionary as long as the keys, and values matches. (Dictionary has no order in Python)

>>> {'a': 1, 'b': 2} == {'b': 2, 'a': 1}True

But order is important in list; sorting will solve the problem for the lists.

>>> [1, 2] == [2, 1]False>>> [1, 2] == sorted([2, 1])True

>>> a = '{"errors": [{"error": "invalid", "field": "email"}, {"error": "required", "field": "name"}], "success": false}'>>> b = '{"errors": [{"error": "required", "field": "name"}, {"error": "invalid", "field": "email"}], "success": false}'>>> a, b = json.loads(a), json.loads(b)>>> a['errors'].sort()>>> b['errors'].sort()>>> a == bTrue

Above example will work for the JSON in the question. For general solution, see Zero Piraeus's answer.