Python · Intermediate · 35 min
Separate valid events from bad input
Validate an ingestion batch without losing the evidence for rejected rows.
Task
Implement parse_events(lines). Each input line should be a JSON object containing id and timestamp. Return accepted objects and a list of errors with one-based line numbers.
Requirements
- lines must be a list of strings; otherwise raise ValueError.
- A valid event has a nonempty string id and an integer timestamp (not bool). Negative timestamps are allowed.
- Use error codes invalid_json for malformed JSON and invalid_event for a valid JSON value with the wrong shape or fields.
- Keep valid objects and their extra fields in input order. Return {"events": [...], "errors": [{"line": n, "code": ...}]}. Do not deduplicate valid messages in this parser.
Data qualityJSON parsingQuarantine
Loading exercise…