1r"""Command-line tool to validate and pretty-print JSON 2 3Usage:: 4 5 $ echo '{"json":"obj"}' | python -m json.tool 6 { 7 "json": "obj" 8 } 9 $ echo '{ 1.2:3.4}' | python -m json.tool 10 Expecting property name enclosed in double quotes: line 1 column 3 (char 2) 11 12""" 13import argparse 14import json 15import sys 16from pathlib import Path 17 18 19def main(): 20 prog = 'python -m json.tool' 21 description = ('A simple command line interface for json module ' 22 'to validate and pretty-print JSON objects.') 23 parser = argparse.ArgumentParser(prog=prog, description=description) 24 parser.add_argument('infile', nargs='?', 25 type=argparse.FileType(encoding="utf-8"), 26 help='a JSON file to be validated or pretty-printed', 27 default=sys.stdin) 28 parser.add_argument('outfile', nargs='?', 29 type=Path, 30 help='write the output of infile to outfile', 31 default=None) 32 parser.add_argument('--sort-keys', action='store_true', default=False, 33 help='sort the output of dictionaries alphabetically by key') 34 parser.add_argument('--no-ensure-ascii', dest='ensure_ascii', action='store_false', 35 help='disable escaping of non-ASCII characters') 36 parser.add_argument('--json-lines', action='store_true', default=False, 37 help='parse input using the JSON Lines format. ' 38 'Use with --no-indent or --compact to produce valid JSON Lines output.') 39 group = parser.add_mutually_exclusive_group() 40 group.add_argument('--indent', default=4, type=int, 41 help='separate items with newlines and use this number ' 42 'of spaces for indentation') 43 group.add_argument('--tab', action='store_const', dest='indent', 44 const='\t', help='separate items with newlines and use ' 45 'tabs for indentation') 46 group.add_argument('--no-indent', action='store_const', dest='indent', 47 const=None, 48 help='separate items with spaces rather than newlines') 49 group.add_argument('--compact', action='store_true', 50 help='suppress all whitespace separation (most compact)') 51 options = parser.parse_args() 52 53 dump_args = { 54 'sort_keys': options.sort_keys, 55 'indent': options.indent, 56 'ensure_ascii': options.ensure_ascii, 57 } 58 if options.compact: 59 dump_args['indent'] = None 60 dump_args['separators'] = ',', ':' 61 62 with options.infile as infile: 63 try: 64 if options.json_lines: 65 objs = (json.loads(line) for line in infile) 66 else: 67 objs = (json.load(infile),) 68 69 if options.outfile is None: 70 out = sys.stdout 71 else: 72 out = options.outfile.open('w', encoding='utf-8') 73 with out as outfile: 74 for obj in objs: 75 json.dump(obj, outfile, **dump_args) 76 outfile.write('\n') 77 except ValueError as e: 78 raise SystemExit(e) 79 80 81if __name__ == '__main__': 82 try: 83 main() 84 except BrokenPipeError as exc: 85 sys.exit(exc.errno) 86