Created
January 10, 2018 14:24
-
-
Save olehermanse/cfb2bb1cdc62945b94171d1c99e71244 to your computer and use it in GitHub Desktop.
Recursively sorting json in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import json | |
from collections import OrderedDict | |
def dict_sort(old_dict): | |
if isinstance(old_dict, list): | |
return [dict_sort(x) for x in old_dict] | |
if not isinstance(old_dict, dict): | |
return old_dict | |
new_dict = OrderedDict() | |
for key in list(sorted(old_dict)): | |
value = old_dict[key] | |
new_dict[key] = dict_sort(value) | |
return new_dict | |
for filename in sys.argv[1:]: | |
with open(filename, 'r') as fp: | |
file_string = fp.read() | |
old_json = json.loads(file_string, object_pairs_hook=OrderedDict) | |
new_json = dict_sort(old_json) | |
file_string = json.dumps(new_json, indent=2) | |
with open(filename+".out.json", 'w') as fp: | |
fp.write(file_string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment