Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Flatten Nested JSON to Table
00:00
5 left

Flatten Nested JSON to Table

HardPython

Problem

LSEG Workspace event payloads can contain nested objects and arrays that are difficult to process directly. Write a function that converts one nested JSON object into a list of flat row objects.

Formal Specification

Implement flatten_json(obj):

  • Input: obj, a JSON object represented as a Python dictionary. Values may be dictionaries, lists, strings, numbers, booleans, or None.
  • Output: a list of dictionaries, where every leaf value is stored under a dot-separated key path.
  • Nested dictionary keys must be joined with .. For example, {"quote": {"bid": 101}} becomes { "quote.bid": 101 }.
  • A list expands into multiple output rows. Each element contributes values at the same path.
  • If multiple lists occur in one object, produce the Cartesian product of their elements and repeat scalar values in every resulting row.
  • An empty list contributes one row with value None, preserving the path.
  • Input object keys do not contain .. Preserve input order when producing rows.

Constraints

  • 1 <= number of input keys <= 100
  • Maximum nesting depth is 20
  • Total number of JSON nodes is at most 10^4
  • Array expansion may produce at most 10^4 output rows
  • Object keys are non-empty strings and contain no .

Function Signature

def flatten_json(obj):
Interviewer

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