Your question is JSON Parsing, Validation, Transformation. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
DataBank Holdings receives monitoring events from a legacy system with inconsistent field formats. Write parse_monitoring_payload(payload) to validate each event, transform valid events into a canonical representation, and report invalid or duplicate events without stopping the batch.
The input is a Python dictionary decoded from JSON. It contains an events array. Each event must include string fields id, host, metric, unit, severity, and timestamp, plus a numeric value. Severity values are INFO, WARN, or CRIT. Timestamps must be UTC ISO 8601 strings ending in Z.
Return a dictionary with accepted and errors arrays. Preserve input order. Each accepted event must contain the stripped host, a lowercase underscore-separated metric name, a floating-point value, a lowercase unit, a normalized severity of info, warning, or critical, a Unix timestamp in seconds, and normalized labels. An event is invalid if a required field is missing, a value cannot be converted to a finite number, a timestamp is invalid, a label is not a string pair, or its normalized ID was already accepted.
Each error contains the event's zero-based index, its id when available, and a short reason.
def parse_monitoring_payload(payload):