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) |
| 23 | parser.add_argument('infile', nargs='?', type=argparse.FileType(), |
| 24 | help='a JSON file to be validated or pretty-printed') |
| 25 | parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), |
| 26 | help='write the output of infile to outfile') |
Berker Peksag | 39e4c4d | 2014-11-10 09:56:54 +0200 | [diff] [blame] | 27 | parser.add_argument('--sort-keys', action='store_true', default=False, |
| 28 | help='sort the output of dictionaries alphabetically by key') |
HongWeipeng | f194479 | 2018-11-07 18:09:32 +0800 | [diff] [blame^] | 29 | parser.add_argument('--json-lines', action='store_true', default=False, |
| 30 | help='parse input using the jsonlines format') |
Benjamin Peterson | 940e207 | 2014-03-21 23:17:29 -0500 | [diff] [blame] | 31 | options = parser.parse_args() |
| 32 | |
| 33 | infile = options.infile or sys.stdin |
| 34 | outfile = options.outfile or sys.stdout |
Berker Peksag | 39e4c4d | 2014-11-10 09:56:54 +0200 | [diff] [blame] | 35 | sort_keys = options.sort_keys |
HongWeipeng | f194479 | 2018-11-07 18:09:32 +0800 | [diff] [blame^] | 36 | json_lines = options.json_lines |
| 37 | with infile, outfile: |
Ezio Melotti | 057bcb4 | 2012-11-29 02:15:18 +0200 | [diff] [blame] | 38 | try: |
HongWeipeng | f194479 | 2018-11-07 18:09:32 +0800 | [diff] [blame^] | 39 | if json_lines: |
| 40 | objs = (json.loads(line) for line in infile) |
| 41 | else: |
| 42 | objs = (json.load(infile), ) |
| 43 | for obj in objs: |
| 44 | json.dump(obj, outfile, sort_keys=sort_keys, indent=4) |
| 45 | outfile.write('\n') |
Ezio Melotti | 057bcb4 | 2012-11-29 02:15:18 +0200 | [diff] [blame] | 46 | except ValueError as e: |
| 47 | raise SystemExit(e) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | if __name__ == '__main__': |
| 51 | main() |