from collections import OrderedDict as OD

def order(X):
    retval = OD()
for k in sorted(X):
    if isinstance(X[k], dict):
        retval[k] = order(X[k])
else:
    retval[k] = X[k]
return retval
Python полный порядок вложенного dict