8

I have formatted my String to look like JSON so I could do json.loads on it. When I printed on the screen it turned out it messed up the order. I know that Python dictonaries are not ordered but is there ANY way to keeps this order? I really need to keep it. Thanks!

shurrok
  • 705
  • 2
  • 10
  • 34
  • Well it does not make any sense in the first place since both Python and JSON dictionaries are unordered. – Willem Van Onsem Nov 04 '17 at 13:07
  • I have read that there is something like `OrderedDict` in Python, wouldn't that be helpful? – shurrok Nov 04 '17 at 13:08
  • https://stackoverflow.com/questions/6921699/can-i-get-json-to-load-into-an-ordereddict-in-python – Abdul Niyas P M Nov 04 '17 at 13:10
  • *"formatted my String to look like JSON"* - That sounds like you're doing it in a bad way... – Stefan Pochmann Nov 04 '17 at 13:12
  • @StefanPochmann why do you say that? :) I needed to do that to use `json.loads` on it – shurrok Nov 04 '17 at 13:13
  • @soommy12 Because it sounds like you're running your own formatting code instead of just using `json.dumps`. – Stefan Pochmann Nov 04 '17 at 13:13
  • @StefanPochmann I have a String cut from `JavaScript` code, it was looking similar to `JSON` but I needed to format it to use `json.loads`. I think this is the only way. But if you have any other idea let me know, maybe I can improve my code – shurrok Nov 04 '17 at 13:16
  • @soommy12 Cutting from JavaScript code also sounds odd... If you have the data in JavaScript, I'd say have the JavaScript convert it to JSON. Then again, I don't really know what you're doing and I don't know much about JavaScript. – Stefan Pochmann Nov 04 '17 at 13:22
  • 1
    @StefanPochmann well, trust me this is the only way I can do my project, I do not need to tell you exactly what I am doing :P question was about keeping order in `json.loads()` only and problem is solved, no need for more discussion – shurrok Nov 04 '17 at 13:50

1 Answers1

20

Both JSON en Python dictionaries (those are JSON objects) are unordered. So in fact it does not makes any sense to do that, because the JSON encoder can change the order.

You can however define a custom JSON decoder, and then parse it with that decoder. So here the dictionary hook willl be an OrderedDict:

from json import JSONDecoder
from collections import OrderedDict

customdecoder = JSONDecoder(object_pairs_hook=OrderedDict)

Then you can decode with:

customdecoder.decode(your_json_string)

This will thus store the items in an OrderedDict instead of a dictionary. But be aware - as said before - that the order of the keys of JSON objects is unspecified.

Alternatively, you can also pass the hook to the loads function:

from json import loads
from collections import OrderedDict

loads(your_json_string, object_pairs_hook=OrderedDict)

Update: as of , a dictionary retains insertion order. So if one uses , the standard json.load and json.loads should work fine. Note however that a JSON object is still unordered, so that the JavaScript end can load/dump the object in any order.

Willem Van Onsem
  • 321,217
  • 26
  • 295
  • 405