diff options
author | Daniel Narvaez <dwnarvaez@gmail.com> | 2012-11-26 17:40:47 (GMT) |
---|---|---|
committer | Daniel Narvaez <dwnarvaez@gmail.com> | 2012-11-26 17:40:47 (GMT) |
commit | 48fa680ef5982cc59d1f4683aeed2865be7225bf (patch) | |
tree | a436bfaac74cf6651b5aa9ceb7a08abbf4afd34b /commands/helpers | |
parent | d17c4aa7fce1fa4cdcc4d39d675042b87b579067 (diff) |
Add a target to normalize json
And run it
Diffstat (limited to 'commands/helpers')
-rwxr-xr-x | commands/helpers/json-normalize | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/commands/helpers/json-normalize b/commands/helpers/json-normalize new file mode 100755 index 0000000..ec6bd0f --- /dev/null +++ b/commands/helpers/json-normalize @@ -0,0 +1,23 @@ +#!/usr/bin/python + +import argparse +import json +from operator import itemgetter + +parser = argparse.ArgumentParser() +parser.add_argument("paths", nargs="+", help="path to the json file") +parser.add_argument("--sort-by", help="dictionary key to sort by") +args = parser.parse_args() + +for path in args.paths: + in_file = open(path, "rb") + data = json.load(in_file) + in_file.close() + + if args.sort_by is not None: + data.sort(key=itemgetter(args.sort_by)) + + out_file = open(path, "wb") + json.dump(data, out_file, sort_keys=True, indent=4) + out_file.write('\n') + out_file.close() |