At Meta, infrastructure and service configs are often represented as nested JSON-like dictionaries. Write a function that flattens a nested dictionary into a single-level dictionary using dot-separated key paths.
Given a dictionary where values may be primitives or other dictionaries, return a new dictionary mapping each leaf value to its full path. If a value is not a dictionary, it is a leaf and should appear in the output. Use . to join nested keys.
obj, a Python dictionary with string keys. Values may be strings, numbers, booleans, null-like values, lists, or nested dictionaries.Example 1
Input: obj = {"service": {"name": "feed", "port": 8080}, "region": "us-east"}
Output: {"service.name": "feed", "service.port": 8080, "region": "us-east"}
Explanation: Nested keys under service are expanded into dot-separated paths.
Example 2
Input: obj = {"a": {"b": {"c": 1}}, "d": 2}
Output: {"a.b.c": 1, "d": 2}
Explanation: The nested chain under a becomes one flattened key.
0 <= len(obj) <= 10^4 total key-value pairs across all nested dictionaries.10^3