Ok so I can use an OrderedDict in json.dump
. That is, an OrderedDict can be used as an input to JSON.
But can it be used as an output? If so how? In my case I'd like to load
into an OrderedDict so I can keep the order of the keys in the file.
If not, is there some kind of workaround?
Best Answer
Yes, you can. By specifying the
object_pairs_hook
argument to JSONDecoder. In fact, this is the exact example given in the documentation.You can pass this parameter to
json.loads
(if you don't need a Decoder instance for other purposes) like so:Using
json.load
is done in the same way: