
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.
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:
.[i]str, int, float, bool, None) become final entriesNoneExample 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.
0 and 10^50 and 10^3. or bracket charactersdata = {"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.data = {"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.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 charactersdef flatten_json(data):