From 007a6a839e3ee6b2e21b740ae5ea694ddbc0d405 Mon Sep 17 00:00:00 2001 From: Daniel Narvaez Date: Sat, 05 Jan 2013 00:53:49 +0000 Subject: More appropriate naming --- (limited to 'tools/json-format') diff --git a/tools/json-format b/tools/json-format new file mode 100755 index 0000000..069908d --- /dev/null +++ b/tools/json-format @@ -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() -- cgit v0.9.1