Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

JSON Parsing, Validation, Transformation

MediumPython00:00
Practice interviewer
In session
5 left
00:00

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.

You need to log in / sign up to run or submit.

Problem

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.

Constraints

  • 1 <= len(payload["events"]) <= 10^4
  • Each event is a JSON object
  • IDs, hosts, metrics, and units contain at most 200 characters
  • Labels contain at most 20 string key-value pairs
  • Timestamps are UTC ISO 8601 strings ending in Z

Function Signature

def parse_monitoring_payload(payload):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output