Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Validating Complex JSON Responses

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

Your question is Validating Complex JSON Responses. 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

Pureintegration QA tests need to verify that an integration response has the expected nested JSON structure. Implement a recursive validator that checks objects, arrays, primitive types, required fields, and minimum array lengths against a supplied schema.

Formal Specification

Write validate_response(response, schema):

  • response is a JSON-compatible Python value containing dictionaries, lists, strings, numbers, booleans, or None.
  • schema is a dictionary node with a type field.
  • An object schema uses properties, a dictionary mapping field names to child schemas, and an optional required list.
  • An array schema uses an items child schema and may define minItems.
  • Primitive schemas use types string, integer, number, boolean, or null.
  • Return True only when the complete response satisfies the schema. Unknown object fields are allowed. Return False for missing required fields, incorrect types, invalid nested values, or invalid array lengths.

A JSON boolean must not be accepted as an integer, even though Python makes bool a subclass of int.

Constraints

  • The response and schema contain at most 10^4 total nodes.
  • Nesting depth is at most 100.
  • Object field names are strings.
  • Schemas use object, array, string, integer, number, boolean, and null node types.
  • Unknown response object fields are allowed.

Function Signature

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