Both JSON en Python dictionaries (these are JSON objects) are unordered. So it really makes no sense, because the JSON encoder can reorder.
However, you can define a custom JSON decoder and then parse it using this decoder. Therefore, here the vocabulary hook will be 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)
, OrderedDict
. - , JSON .
loads
:
from json import loads
from collections import OrderedDict
loads(your_json_string, object_pairs_hook=OrderedDict)