AdvancedChallenge 31 of 34

Flatten JSON

Write flatten_json(obj) that turns nested dicts and into a single-level dict whose keys are dotted paths. List elements use their index as a key segment. Scalars keep their values.

Examples
flatten_json({"a": {"b": 1, "c": [10, 20]}, "d": "x"}) → {"a.b": 1, "a.c.0": 10, "a.c.1": 20, "d": "x"}
Exercise

Flatten JSON

4 checks
def flatten_json(obj):
    ...


print(flatten_json({"a": {"b": 1, "c": [10, 20]}, "d": "x"}))
 
Python (loads on first run)Ctrl+Enter runs
Output
Press Run to try your code, then Check answer to grade it.