Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
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
Sign up free to run your codeLog inLn 2
Run your code to see test output here.