Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Parse and Clean Nested JSON

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

Your question is Parse and Clean 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

TetraScience assay payloads can contain measurements nested under experiments, samples, panels, and other JSON containers. Write a function that recursively extracts valid measurement objects and returns a clean, flat list while preserving the nearest assay, experiment, and sample identifiers.

A measurement object is any JSON object containing both analyte and value. Normalize each valid measurement as follows:

  1. Trim whitespace from string fields.
  2. Convert analyte to lowercase.
  3. Convert numeric values and numeric strings to Python float values.
  4. Convert non-empty unit strings to uppercase. Use None when unit is missing or blank.
  5. Skip measurements with a missing or blank analyte, a missing value, or a value that cannot be converted to a finite number.
  6. Include the nearest inherited assay_id, experiment_id, and sample_id. Use None when an identifier is unavailable.

Formal Specification

Input payload is a JSON-compatible Python dictionary containing nested dictionaries, lists, strings, numbers, booleans, or None. Return a list of dictionaries with exactly these keys: assay_id, experiment_id, sample_id, analyte, value, and unit. Preserve depth-first traversal order. Identifiers found on a nested object override inherited identifiers for that object's descendants.

Constraints

  • 1 <= number of JSON nodes <= 10^5
  • Nesting depth is at most 500
  • Each measurement contains at most one analyte and value field
  • Values are JSON-compatible primitives
  • Output preserves depth-first traversal order

Function Signature

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