--- /dev/null
+r"""Command-line tool to validate and pretty-print JSON\r
+\r
+Usage::\r
+\r
+ $ echo '{"json":"obj"}' | python -m json.tool\r
+ {\r
+ "json": "obj"\r
+ }\r
+ $ echo '{ 1.2:3.4}' | python -m json.tool\r
+ Expecting property name enclosed in double quotes: line 1 column 3 (char 2)\r
+\r
+"""\r
+import sys\r
+import json\r
+\r
+def main():\r
+ if len(sys.argv) == 1:\r
+ infile = sys.stdin\r
+ outfile = sys.stdout\r
+ elif len(sys.argv) == 2:\r
+ infile = open(sys.argv[1], 'rb')\r
+ outfile = sys.stdout\r
+ elif len(sys.argv) == 3:\r
+ infile = open(sys.argv[1], 'rb')\r
+ outfile = open(sys.argv[2], 'wb')\r
+ else:\r
+ raise SystemExit(sys.argv[0] + " [infile [outfile]]")\r
+ with infile:\r
+ try:\r
+ obj = json.load(infile)\r
+ except ValueError, e:\r
+ raise SystemExit(e)\r
+ with outfile:\r
+ json.dump(obj, outfile, sort_keys=True,\r
+ indent=4, separators=(',', ': '))\r
+ outfile.write('\n')\r
+\r
+\r
+if __name__ == '__main__':\r
+ main()\r