Parse JSON into ordered data-structure (in python)
In many cases it is essential (or at the least nicer) to preserve key order from a parsed JSON document, here is how to do it in python (using the std lib json
module and OrderedDict
available in python 2.7+
)
from collections import OrderedDict
import json
r = json.load(open('file.json'), object_pairs_hook=OrderedDict)
print json.dumps(r, indent=2)
Written by Hatem Nassrat
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#