diff options
author | Brett Cannon <bcannon@gmail.com> | 2008-05-05 20:21:38 +0000 |
---|---|---|
committer | Brett Cannon <bcannon@gmail.com> | 2008-05-05 20:21:38 +0000 |
commit | 4b964f9c904744b7d7d88054e54a2e4ca8aeb395 (patch) | |
tree | c24ba51492fc3fc714c54f2e671f21172d063908 /Lib/json/tool.py | |
parent | 5f2e0e5ccb988cdf65137034b33ee57198cc23b9 (diff) | |
download | cpython-git-4b964f9c904744b7d7d88054e54a2e4ca8aeb395.tar.gz |
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Ippolito.
Closes issue #2750.
Diffstat (limited to 'Lib/json/tool.py')
-rw-r--r-- | Lib/json/tool.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/Lib/json/tool.py b/Lib/json/tool.py new file mode 100644 index 0000000000..cbd258ce24 --- /dev/null +++ b/Lib/json/tool.py @@ -0,0 +1,36 @@ +r"""Command-line tool to validate and pretty-print JSON + +Usage:: + + $ echo '{"json":"obj"}' | python -mjson.tool + { + "json": "obj" + } + $ echo '{ 1.2:3.4}' | python -mjson.tool + Expecting property name: line 1 column 2 (char 2) +""" +import sys +import json + +def main(): + if len(sys.argv) == 1: + infile = sys.stdin + outfile = sys.stdout + elif len(sys.argv) == 2: + infile = open(sys.argv[1], 'rb') + outfile = sys.stdout + elif len(sys.argv) == 3: + infile = open(sys.argv[1], 'rb') + outfile = open(sys.argv[2], 'wb') + else: + raise SystemExit("{0} [infile [outfile]]".format(sys.argv[0])) + try: + obj = json.load(infile) + except ValueError, e: + raise SystemExit(e) + json.dump(obj, outfile, sort_keys=True, indent=4) + outfile.write('\n') + + +if __name__ == '__main__': + main() |