Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Python JSON Nested Field Validation
00:00
5 left

Python JSON Nested Field Validation

MediumPython

Problem

Write a Python script to parse an API JSON response and validate specific nested fields.

Implement validate_api_response(json_response, expected_fields). The first argument is a JSON string, and the second is a dictionary mapping dot-delimited paths to expected values. Return True only when the JSON is valid and every expected field exists with the specified value. Return False for malformed JSON, missing paths, or mismatched values. Numeric path components address list elements, such as items.0.id.

Constraints

  • json_response is a JSON string representing an object or array.
  • expected_fields contains dot-delimited paths mapped to JSON-compatible expected values.
  • Path components are non-empty strings, and numeric components represent zero-based list indices.
  • Return False for malformed JSON, missing fields, invalid list indices, or mismatched values.

Function Signature

def validate_api_response(json_response, expected_fields):
Interviewer

Your question is Python JSON Nested Field Validation. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.