validate_json.py 812 B

12345678910111213141516171819202122232425
  1. import jstyleson
  2. from pathlib import Path
  3. from pprint import pprint
  4. errors = []
  5. for path in sorted(Path('.').glob('**/*.json')):
  6. # because path is an object and not a string
  7. path_str = str(path)
  8. try:
  9. with open(path_str, 'r') as file:
  10. jstyleson.load(file)
  11. print(f"Validation of {path_str} succeeded")
  12. except Exception as exc:
  13. print(f"Validation of {path_str} failed")
  14. pprint(exc)
  15. # https://stackoverflow.com/a/72850269/2278742
  16. if hasattr(exc, 'pos'):
  17. position_msg = f"{path_str}:{exc.lineno}:{exc.colno}"
  18. print(position_msg)
  19. errors.append({"position": position_msg, "exception": exc})
  20. if errors:
  21. print("Summary of errors:")
  22. pprint(errors)
  23. raise Exception("Not all JSON files are valid")