Items in JSON object are out of order using "json.dumps"? Items in JSON object are out of order using "json.dumps"? json json

Items in JSON object are out of order using "json.dumps"?

Both Python dict (before Python 3.7) and JSON object are unordered collections. You could pass sort_keys parameter, to sort the keys:

>>> import json>>> json.dumps({'a': 1, 'b': 2})'{"b": 2, "a": 1}'>>> json.dumps({'a': 1, 'b': 2}, sort_keys=True)'{"a": 1, "b": 2}'

If you need a particular order; you could use collections.OrderedDict:

>>> from collections import OrderedDict>>> json.dumps(OrderedDict([("a", 1), ("b", 2)]))'{"a": 1, "b": 2}'>>> json.dumps(OrderedDict([("b", 2), ("a", 1)]))'{"b": 2, "a": 1}'

Since Python 3.6, the keyword argument order is preserved and the above can be rewritten using a nicer syntax:

>>> json.dumps(OrderedDict(a=1, b=2))'{"a": 1, "b": 2}'>>> json.dumps(OrderedDict(b=2, a=1))'{"b": 2, "a": 1}'

See PEP 468 – Preserving Keyword Argument Order.

If your input is given as JSON then to preserve the order (to get OrderedDict), you could pass object_pair_hook, as suggested by @Fred Yankowski:

>>> json.loads('{"a": 1, "b": 2}', object_pairs_hook=OrderedDict)OrderedDict([('a', 1), ('b', 2)])>>> json.loads('{"b": 2, "a": 1}', object_pairs_hook=OrderedDict)OrderedDict([('b', 2), ('a', 1)])

As others have mentioned the underlying dict is unordered. However there are OrderedDict objects in python. ( They're built in in recent pythons, or you can use this: ).

I believe that newer pythons json implementations correctly handle the built in OrderedDicts, but I'm not sure (and I don't have easy access to test).

Old pythons simplejson implementations dont handle the OrderedDict objects nicely .. and convert them to regular dicts before outputting them.. but you can overcome this by doing the following:

class OrderedJsonEncoder( simplejson.JSONEncoder ):   def encode(self,o):      if isinstance(o,OrderedDict.OrderedDict):         return "{" + ",".join( [ self.encode(k)+":"+self.encode(v) for (k,v) in o.iteritems() ] ) + "}"      else:         return simplejson.JSONEncoder.encode(self, o)

now using this we get:

>>> import OrderedDict>>> unordered={"id":123,"name":"a_name","timezone":"tz"}>>> ordered = OrderedDict.OrderedDict( [("id",123), ("name","a_name"), ("timezone","tz")] )>>> e = OrderedJsonEncoder()>>> print e.encode( unordered ){"timezone": "tz", "id": 123, "name": "a_name"}>>> print e.encode( ordered ){"id":123,"name":"a_name","timezone":"tz"}

Which is pretty much as desired.

Another alternative would be to specialise the encoder to directly use your row class, and then you'd not need any intermediate dict or UnorderedDict.

hey i know it is so late for this answer but add sort_keys and assign false to it as follows :

json.dumps({'****': ***},sort_keys=False)

this worked for me