blob: c42138a05c05e7b6a136a490f7e7b1f43a2a85c5 [file] [log] [blame]
Christian Heimes90540002008-05-08 14:29:10 +00001r"""Command-line tool to validate and pretty-print JSON
2
3Usage::
4
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00005 $ echo '{"json":"obj"}' | python -m json.tool
Christian Heimes90540002008-05-08 14:29:10 +00006 {
7 "json": "obj"
8 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00009 $ echo '{ 1.2:3.4}' | python -m json.tool
Serhiy Storchakac510a042013-02-21 20:19:16 +020010 Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
Christian Heimes90540002008-05-08 14:29:10 +000011
12"""
Benjamin Peterson940e2072014-03-21 23:17:29 -050013import argparse
Christian Heimes90540002008-05-08 14:29:10 +000014import json
Benjamin Peterson940e2072014-03-21 23:17:29 -050015import sys
16
Christian Heimes90540002008-05-08 14:29:10 +000017
18def main():
Benjamin Peterson940e2072014-03-21 23:17:29 -050019 prog = 'python -m json.tool'
20 description = ('A simple command line interface for json module '
21 'to validate and pretty-print JSON objects.')
22 parser = argparse.ArgumentParser(prog=prog, description=description)
Miss Islington (bot)a75cad42019-12-04 01:57:55 -080023 parser.add_argument('infile', nargs='?',
24 type=argparse.FileType(encoding="utf-8"),
Hervé Beraud4d45a3b2019-05-14 18:52:42 +020025 help='a JSON file to be validated or pretty-printed',
26 default=sys.stdin)
Miss Islington (bot)a75cad42019-12-04 01:57:55 -080027 parser.add_argument('outfile', nargs='?',
28 type=argparse.FileType('w', encoding="utf-8"),
Hervé Beraud4d45a3b2019-05-14 18:52:42 +020029 help='write the output of infile to outfile',
30 default=sys.stdout)
Berker Peksag39e4c4d2014-11-10 09:56:54 +020031 parser.add_argument('--sort-keys', action='store_true', default=False,
32 help='sort the output of dictionaries alphabetically by key')
HongWeipengf1944792018-11-07 18:09:32 +080033 parser.add_argument('--json-lines', action='store_true', default=False,
34 help='parse input using the jsonlines format')
Benjamin Peterson940e2072014-03-21 23:17:29 -050035 options = parser.parse_args()
36
Hervé Beraud4d45a3b2019-05-14 18:52:42 +020037 infile = options.infile
38 outfile = options.outfile
Berker Peksag39e4c4d2014-11-10 09:56:54 +020039 sort_keys = options.sort_keys
HongWeipengf1944792018-11-07 18:09:32 +080040 json_lines = options.json_lines
41 with infile, outfile:
Ezio Melotti057bcb42012-11-29 02:15:18 +020042 try:
HongWeipengf1944792018-11-07 18:09:32 +080043 if json_lines:
44 objs = (json.loads(line) for line in infile)
45 else:
46 objs = (json.load(infile), )
47 for obj in objs:
48 json.dump(obj, outfile, sort_keys=sort_keys, indent=4)
49 outfile.write('\n')
Ezio Melotti057bcb42012-11-29 02:15:18 +020050 except ValueError as e:
51 raise SystemExit(e)
Christian Heimes90540002008-05-08 14:29:10 +000052
53
54if __name__ == '__main__':
Dong-hee Nacaec8a02020-03-10 17:14:08 +090055 try:
56 main()
57 except BrokenPipeError as exc:
58 sys.exit(exc.errno)