31 lines
864 B
Python
31 lines
864 B
Python
#!/usr/bin/env python3
|
|
"""Simple JSON validator - validates JSON files and reports errors."""
|
|
|
|
import sys
|
|
import json
|
|
|
|
def validate_json(filepath):
|
|
"""Validate a JSON file. Returns (is_valid, error_message)."""
|
|
try:
|
|
with open(filepath, 'r') as f:
|
|
json.load(f)
|
|
return True, None
|
|
except FileNotFoundError:
|
|
return False, f"File not found: {filepath}"
|
|
except json.JSONDecodeError as e:
|
|
return False, f"Invalid JSON: {e}"
|
|
except Exception as e:
|
|
return False, f"Error: {e}"
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 2:
|
|
print(f"Usage: {sys.argv[0]} <json-file>", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
is_valid, error = validate_json(sys.argv[1])
|
|
if is_valid:
|
|
print("VALID")
|
|
sys.exit(0)
|
|
else:
|
|
print(error, file=sys.stderr)
|
|
sys.exit(1) |