33 lines
926 B
Python
33 lines
926 B
Python
#!/usr/bin/env python3
|
|
"""Validate JSON files - returns 0 if valid, nonzero if invalid or missing 'valid' key."""
|
|
import sys
|
|
import json
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
print("Usage: json_validator.py <file.json>", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
filepath = sys.argv[1]
|
|
|
|
try:
|
|
with open(filepath, 'r') as f:
|
|
data = json.load(f)
|
|
|
|
if isinstance(data, dict) and data.get('valid') == True:
|
|
print(f"VALID: {filepath}")
|
|
sys.exit(0)
|
|
else:
|
|
print(f"INVALID: {filepath} - missing or false 'valid' field", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
except json.JSONDecodeError as e:
|
|
print(f"INVALID: {filepath} - {e}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
except FileNotFoundError:
|
|
print(f"NOT FOUND: {filepath}", file=sys.stderr)
|
|
sys.exit(2)
|
|
|
|
if __name__ == "__main__":
|
|
main() |