1

I need to compare JSON objects which can be as large as 400 kb at times. I've chosen to use Python 3.5 To do this.

Both rapidjson and ultrajson will decode those objects into Python dictionaries and lists quite quickly.

The problem I have is that these dictionaries and lists of dictionaries need to be sorted since JSON objects are unordered.

I came up with this code which sorts as lists of tuples then converts to OrderedDict:

from collections import OrderedDict


def sort_me(unsorted):
    if isinstance(unsorted, dict):
        return sorted((k, sort_me(v)) for k, v in unsorted.items())
    if isinstance(unsorted, list):
        return sorted(sort_me(x) for x in unsorted)
    else:
        return unsorted


def to_ordered_dict(sort_of):
    if isinstance(sort_of, list):
        ordered = OrderedDict()
        if sort_of and isinstance(sort_of[0], list):
            ordered = []
            for i in sort_of:
                ordered.append(to_ordered_dict(i))
            return ordered
        else:
            for i in sort_of:
                if isinstance(i, list):
                    ordered.update(to_ordered_dict(i))
                elif isinstance(i, tuple):
                    ordered[i[0]] = to_ordered_dict(i[1])
            return ordered
    else:
        return sort_of

done = to_ordered_dict(sort_me(large_dict))

This doesn't seem like the best way since I'm going through the objects at least twice. Is there a better way to do this so I'm not looping through the dictionary twice?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
user197674
  • 748
  • 2
  • 7
  • 22
  • http://stackoverflow.com/questions/2710651/javas-treeset-equivalent-in-python I think this is sort of what you're looking for though it is not an official library. Basically, it will sort items as they're added. – fdsa Nov 19 '15 at 04:23

0 Answers0