36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
#!/usr/bin/env python3
|
|
"""JSONL validator - validates each line is valid JSON, reports errors with line numbers."""
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
def validate_jsonl(filepath: str) -> tuple[int, int]:
|
|
"""Validate JSONL file, return (valid_count, error_count)."""
|
|
valid, errors = 0, 0
|
|
with open(filepath, 'r') as f:
|
|
for i, line in enumerate(f, 1):
|
|
line = line.rstrip('\n\r')
|
|
if not line:
|
|
continue # skip empty lines
|
|
try:
|
|
json.loads(line)
|
|
valid += 1
|
|
except json.JSONDecodeError as e:
|
|
errors += 1
|
|
print(f"Line {i}: {e.msg} at column {e.colno}")
|
|
return valid, errors
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) < 2:
|
|
print("Usage: jsonl_validator.py <file.jsonl>")
|
|
sys.exit(1)
|
|
|
|
path = Path(sys.argv[1])
|
|
if not path.exists():
|
|
print(f"Error: file not found: {path}")
|
|
sys.exit(1)
|
|
|
|
valid, errors = validate_jsonl(str(path))
|
|
total = valid + errors
|
|
print(f"\nValidated: {total} lines, {valid} valid, {errors} errors")
|
|
sys.exit(0 if errors == 0 else 1) |