Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Flatten Nested JSON Records

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

Your question is Flatten Nested JSON Records. 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

Given a Python object parsed from JSON, write a function that flattens it into a list of row dictionaries. Nested objects should be flattened using dot-separated keys, and arrays should be expanded by index using bracket notation such as items[0].id. If the input is a single object, return one flattened row; if it is a list of objects, return one flattened row per object.

Constraints

  • 1 <= number of top-level records <= 10^4
  • Total number of primitive leaf values across the payload is at most 2 * 10^5
  • JSON values may be objects, arrays, strings, numbers, booleans, or null

Function Signature

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