Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Flatten Nested JSON Paths

Easy
CodingAsked 1 times

Problem

At Meta, event payloads from products like Facebook and Instagram may contain deeply nested JSON structures. Write a Python function that flattens a nested JSON-like object into a single dictionary of path-value pairs.

Task

Given a Python object data representing parsed JSON, return a flat dictionary where each key is the full path to a primitive value.

Use these rules:

  1. Nested dictionary keys are joined with .
  2. List indices are written as [i]
  3. Primitive values (str, int, float, bool, None) become final entries
  4. Empty dictionaries and empty lists should also be preserved as values at their path

Input / Output

  • Input: a Python object composed only of dictionaries, lists, strings, numbers, booleans, and None
  • Output: a dictionary mapping flattened path strings to values

Constraints

  • The input contains only dictionaries, lists, strings, integers, floats, booleans, and null-like values (None in Python)
  • Total number of dictionary entries and list elements is in the range 0 to 10^5
  • Maximum nesting depth is in the range 0 to 10^3
  • Dictionary keys are non-empty strings without . or bracket characters

Function Signature

def flatten_json(data):

You are practicing as a guest. Sign up free to run your code against the sample data. Your draft stays right here.

Sign up freeI have an account
def solve(rows):
    counts = {}
    for row in rows:
        ...
    return result
Sign up to unlock solutions
Meta Data Engineer Interview Questions
Next questions
McKinsey &Flatten Deeply Nested JSONMediumYelpFlatten Nested Dictionary KeysEasyAkidoFlatten Nested JSONMedium
Python 3.10