Dataford
Interview Guides
Upgrade
All questions/Coding/Flatten Nested JSON Paths

Flatten Nested JSON Paths

Easy
Coding
Asked at 1 company1
Also asked at
Meta

Problem

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

Examples

Example 1 Input: {"user": {"id": 7, "profile": {"name": "Ana"}}, "active": true} Output: {"user.id": 7, "user.profile.name": "Ana", "active": true}

Explanation: Nested object keys are joined with . until a primitive value is reached.

Example 2 Input: {"posts": [{"id": 1}, {"id": 2, "tags": ["ml", "meta"]}]} Output: {"posts[0].id": 1, "posts[1].id": 2, "posts[1].tags[0]": "ml", "posts[1].tags[1]": "meta"}

Explanation: List positions are included in the path using bracket notation.

Constraints

  • Total number of dictionary entries and list elements is between 0 and 10^5
  • Maximum nesting depth is between 0 and 10^3
  • Keys in input dictionaries are non-empty strings without . or bracket characters

Examples

Example 1
Inputdata = {"user": {"id": 7, "profile": {"name": "Ana"}}, "active": True}Output{"user.id": 7, "user.profile.name": "Ana", "active": True}WhyEach nested dictionary key is appended with dot notation until a primitive value is reached.
Example 2
Inputdata = {"posts": [{"id": 1}, {"id": 2, "tags": ["ml", "meta"]}]}Output{"posts[0].id": 1, "posts[1].id": 2, "posts[1].tags[0]": "ml", "posts[1].tags[1]": "meta"}WhyList positions are encoded with bracket indices, so every value keeps its exact location.

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):

Problem

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

Examples

Example 1 Input: {"user": {"id": 7, "profile": {"name": "Ana"}}, "active": true} Output: {"user.id": 7, "user.profile.name": "Ana", "active": true}

Explanation: Nested object keys are joined with . until a primitive value is reached.

Example 2 Input: {"posts": [{"id": 1}, {"id": 2, "tags": ["ml", "meta"]}]} Output: {"posts[0].id": 1, "posts[1].id": 2, "posts[1].tags[0]": "ml", "posts[1].tags[1]": "meta"}

Explanation: List positions are included in the path using bracket notation.

Constraints

  • Total number of dictionary entries and list elements is between 0 and 10^5
  • Maximum nesting depth is between 0 and 10^3
  • Keys in input dictionaries are non-empty strings without . or bracket characters

Examples

Example 1
Inputdata = {"user": {"id": 7, "profile": {"name": "Ana"}}, "active": True}Output{"user.id": 7, "user.profile.name": "Ana", "active": True}WhyEach nested dictionary key is appended with dot notation until a primitive value is reached.
Example 2
Inputdata = {"posts": [{"id": 1}, {"id": 2, "tags": ["ml", "meta"]}]}Output{"posts[0].id": 1, "posts[1].id": 2, "posts[1].tags[0]": "ml", "posts[1].tags[1]": "meta"}WhyList positions are encoded with bracket indices, so every value keeps its exact location.

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):
Practice Python
Python 3.10
Open on desktop for the full Python editor with syntax highlighting and autocomplete.
Up next
MetaFlatten Nested Config DictionaryMediumRFlatten Nested Directory JSON PathsMedium
Next question