Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 1 | r"""Command-line tool to validate and pretty-print JSON |
| 2 | |
| 3 | Usage:: |
| 4 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 5 | $ echo '{"json":"obj"}' | python -m json.tool |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 6 | { |
| 7 | "json": "obj" |
| 8 | } |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 9 | $ echo '{ 1.2:3.4}' | python -m json.tool |
Serhiy Storchaka | c510a04 | 2013-02-21 20:19:16 +0200 | [diff] [blame] | 10 | Expecting property name enclosed in double quotes: line 1 column 3 (char 2) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 11 | |
| 12 | """ |
Benjamin Peterson | 940e207 | 2014-03-21 23:17:29 -0500 | [diff] [blame] | 13 | import argparse |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 14 | import json |
Benjamin Peterson | 940e207 | 2014-03-21 23:17:29 -0500 | [diff] [blame] | 15 | import sys |
| 16 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 17 | |
| 18 | def main(): |
Benjamin Peterson | 940e207 | 2014-03-21 23:17:29 -0500 | [diff] [blame] | 19 | 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) |
Inada Naoki | 808769f | 2019-12-04 18:39:31 +0900 | [diff] [blame] | 23 | parser.add_argument('infile', nargs='?', |
| 24 | type=argparse.FileType(encoding="utf-8"), |
Hervé Beraud | 4d45a3b | 2019-05-14 18:52:42 +0200 | [diff] [blame] | 25 | help='a JSON file to be validated or pretty-printed', |
| 26 | default=sys.stdin) |
Inada Naoki | 808769f | 2019-12-04 18:39:31 +0900 | [diff] [blame] | 27 | parser.add_argument('outfile', nargs='?', |
| 28 | type=argparse.FileType('w', encoding="utf-8"), |
Hervé Beraud | 4d45a3b | 2019-05-14 18:52:42 +0200 | [diff] [blame] | 29 | help='write the output of infile to outfile', |
| 30 | default=sys.stdout) |
Berker Peksag | 39e4c4d | 2014-11-10 09:56:54 +0200 | [diff] [blame] | 31 | parser.add_argument('--sort-keys', action='store_true', default=False, |
| 32 | help='sort the output of dictionaries alphabetically by key') |
wim glenn | efefe25 | 2019-12-06 00:44:01 -0600 | [diff] [blame] | 33 | parser.add_argument('--no-ensure-ascii', dest='ensure_ascii', action='store_false', |
| 34 | help='disable escaping of non-ASCII characters') |
HongWeipeng | f194479 | 2018-11-07 18:09:32 +0800 | [diff] [blame] | 35 | parser.add_argument('--json-lines', action='store_true', default=False, |
Daniel Himmelstein | 15fb7fa | 2019-12-07 07:14:40 -0700 | [diff] [blame] | 36 | help='parse input using the JSON Lines format. ' |
| 37 | 'Use with --no-indent or --compact to produce valid JSON Lines output.') |
Daniel Himmelstein | 0325794 | 2019-12-04 01:15:19 -0500 | [diff] [blame] | 38 | group = parser.add_mutually_exclusive_group() |
| 39 | group.add_argument('--indent', default=4, type=int, |
| 40 | help='separate items with newlines and use this number ' |
| 41 | 'of spaces for indentation') |
| 42 | group.add_argument('--tab', action='store_const', dest='indent', |
| 43 | const='\t', help='separate items with newlines and use ' |
| 44 | 'tabs for indentation') |
| 45 | group.add_argument('--no-indent', action='store_const', dest='indent', |
| 46 | const=None, |
| 47 | help='separate items with spaces rather than newlines') |
| 48 | group.add_argument('--compact', action='store_true', |
| 49 | help='suppress all whitespace separation (most compact)') |
Benjamin Peterson | 940e207 | 2014-03-21 23:17:29 -0500 | [diff] [blame] | 50 | options = parser.parse_args() |
| 51 | |
Daniel Himmelstein | 0325794 | 2019-12-04 01:15:19 -0500 | [diff] [blame] | 52 | dump_args = { |
| 53 | 'sort_keys': options.sort_keys, |
| 54 | 'indent': options.indent, |
wim glenn | efefe25 | 2019-12-06 00:44:01 -0600 | [diff] [blame] | 55 | 'ensure_ascii': options.ensure_ascii, |
Daniel Himmelstein | 0325794 | 2019-12-04 01:15:19 -0500 | [diff] [blame] | 56 | } |
| 57 | if options.compact: |
| 58 | dump_args['indent'] = None |
| 59 | dump_args['separators'] = ',', ':' |
| 60 | |
| 61 | with options.infile as infile, options.outfile as outfile: |
Ezio Melotti | 057bcb4 | 2012-11-29 02:15:18 +0200 | [diff] [blame] | 62 | try: |
Daniel Himmelstein | 0325794 | 2019-12-04 01:15:19 -0500 | [diff] [blame] | 63 | if options.json_lines: |
HongWeipeng | f194479 | 2018-11-07 18:09:32 +0800 | [diff] [blame] | 64 | objs = (json.loads(line) for line in infile) |
| 65 | else: |
| 66 | objs = (json.load(infile), ) |
| 67 | for obj in objs: |
Daniel Himmelstein | 0325794 | 2019-12-04 01:15:19 -0500 | [diff] [blame] | 68 | json.dump(obj, outfile, **dump_args) |
HongWeipeng | f194479 | 2018-11-07 18:09:32 +0800 | [diff] [blame] | 69 | outfile.write('\n') |
Ezio Melotti | 057bcb4 | 2012-11-29 02:15:18 +0200 | [diff] [blame] | 70 | except ValueError as e: |
| 71 | raise SystemExit(e) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 72 | |
| 73 | |
| 74 | if __name__ == '__main__': |
Dong-hee Na | 700cb58 | 2020-03-10 16:41:44 +0900 | [diff] [blame] | 75 | try: |
| 76 | main() |
| 77 | except BrokenPipeError as exc: |
| 78 | sys.exit(exc.errno) |