Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Validate JSON Against Schema

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

Your question is Validate JSON Against Schema. 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

Netflix Keystone pipelines receive nested JSON payloads. Implement validate_json(data, schema) to determine whether a JSON-compatible value conforms to a supported schema.

Formal Specification

data is a JSON-compatible Python value: None, bool, int, float, str, list, or dict. schema is a dictionary supporting these keywords:

  1. type: one of null, boolean, number, integer, string, array, or object.
  2. enum: an array of permitted values. The data must equal one of them when present.
  3. properties: an object mapping property names to child schemas. These schemas are checked when the properties exist.
  4. required: an array of property names that must exist when the value is an object.
  5. items: a schema applied to every element when the value is an array.

Return True if every applicable rule passes, otherwise return False. Object properties not listed in properties are allowed. A schema without a keyword imposes no restriction at that level.

Constraints

  • The value contains at most 10^4 total nested values
  • Nesting depth is at most 500
  • Supported schema keywords are type, enum, properties, required, and items
  • Object properties not listed in properties are allowed
  • Inputs contain only valid JSON-compatible values

Function Signature

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