jsondiff 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/tmp/build/80754af9/jsonpatch_1615747632069/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_plac/bin/python
  2. # -*- coding: utf-8 -*-
  3. from __future__ import print_function
  4. import sys
  5. import json
  6. import jsonpatch
  7. import argparse
  8. parser = argparse.ArgumentParser(description='Diff two JSON files')
  9. parser.add_argument('FILE1', type=argparse.FileType('r'))
  10. parser.add_argument('FILE2', type=argparse.FileType('r'))
  11. parser.add_argument('--indent', type=int, default=None,
  12. help='Indent output by n spaces')
  13. parser.add_argument('-v', '--version', action='version',
  14. version='%(prog)s ' + jsonpatch.__version__)
  15. def main():
  16. try:
  17. diff_files()
  18. except KeyboardInterrupt:
  19. sys.exit(1)
  20. def diff_files():
  21. """ Diffs two JSON files and prints a patch """
  22. args = parser.parse_args()
  23. doc1 = json.load(args.FILE1)
  24. doc2 = json.load(args.FILE2)
  25. patch = jsonpatch.make_patch(doc1, doc2)
  26. if patch.patch:
  27. print(json.dumps(patch.patch, indent=args.indent))
  28. sys.exit(1)
  29. if __name__ == "__main__":
  30. main()