Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Parse and Validate Nested JSON

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

Your question is Parse and Validate Nested JSON. 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

Commure clinical workflows exchange nested JSON event payloads. Implement a validator that determines whether a parsed JSON value conforms to a recursively defined schema.

Formal Specification

Write validate_payload(value, schema):

  • value is a parsed JSON value represented by Python dictionaries, lists, strings, numbers, booleans, or None.
  • schema is a dictionary with a required type field: object, array, string, number, boolean, or null.
  • An object schema may contain required, properties, and additionalProperties. If additionalProperties is false, keys absent from properties are invalid.
  • An array schema may contain items, which defines the schema for every element.
  • Any schema may contain enum; the value must equal one of the listed values when present.
  • Return True only when the complete value satisfies every applicable rule. Return False otherwise.

Constraints

  • The value contains at most 10^4 objects and arrays combined.
  • Nesting depth is at most 1000.
  • Schema nodes are valid and use only the supported fields.
  • Object keys are strings, and schemas do not contain recursive references.

Function Signature

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