I'm using json.dumps
to convert into json like
countries.append({"id":row.id,"name":row.name,"timezone":row.timezone})
print json.dumps(countries)
The result i have is:
[
{"timezone": 4, "id": 1, "name": "Mauritius"},
{"timezone": 2, "id": 2, "name": "France"},
{"timezone": 1, "id": 3, "name": "England"},
{"timezone": -4, "id": 4, "name": "USA"}
]
I want to have the keys in the following order: id, name, timezone - but instead I have timezone, id, name.
How should I fix this?
Both Python
dict
(before Python 3.7) and JSON object are unordered collections. You could passsort_keys
parameter, to sort the keys:If you need a particular order; you could use
collections.OrderedDict
:Since Python 3.6, the keyword argument order is preserved and the above can be rewritten using a nicer syntax:
See PEP 468 – Preserving Keyword Argument Order.
If your input is given as JSON then to preserve the order (to get
OrderedDict
), you could passobject_pair_hook
, as suggested by @Fred Yankowski: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: http://code.activestate.com/recipes/576693/ ).
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:
now using this we get:
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.
json.dump() will preserve the ordder of your dictionary. Open the file in a text editor and you will see. It will preserve the order regardless of whether you send it an OrderedDict.
But json.load() will lose the order of the saved object unless you tell it to load into an OrderedDict(), which is done with the object_pairs_hook parameter as J.F.Sebastian instructed above.
It would otherwise lose the order because under usual operation, it loads the saved dictionary object into a regular dict and a regular dict does not preserve the oder of the items it is given.
in JSON, as in Javascript, order of object keys is meaningless, so it really doesn't matter what order they're displayed in, it is the same object.
The order of a dictionary doesn't have any relationship to the order it was defined in. This is true of all dictionaries, not just those turned into JSON.
Indeed, the dictionary was turned "upside down" before it even reached
json.dumps
: