tool.py 3.3 KB

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