Skip to content

Instantly share code, notes, and snippets.

@warvariuc
Last active October 24, 2023 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warvariuc/4dd64290e6681e45462bde15efcd516a to your computer and use it in GitHub Desktop.
Save warvariuc/4dd64290e6681e45462bde15efcd516a to your computer and use it in GitHub Desktop.
def flatten_dict(data: dict):
"""Transform a dict with nested dicts/list to a one level dict with keys containing paths to
the original values encoded as `top_level_key.nested_key[list_item_index]`
"""
def iterate(_data, _path=""):
if isinstance(_data, dict):
for _key, _value in _data.items():
yield from iterate(_value, f"{_path}.{_key}".lstrip("."))
elif isinstance(_data, tuple | list):
for _index, _value in enumerate(_data):
yield from iterate(_value, f"{_path}[{_index}]")
else:
yield _path, _data
return dict(iterate(data))
def test_flatten_dict():
assert flatten_dict(
{
"_id": "pra_2tV33hckuMLTKm9nXqnRbE",
"address": {
"city": "Swindon",
"country": "UK",
"county": "Wiltshire",
"line_1": "Unit 10 Berkshire House",
"line_2": "",
"post_code": "SN1 2NR",
},
"metadata": {
"integration_ids": [
{"key": "vet_covea_id", "value": "127258", "source": "CATDOG"},
{"key": "vet_algolia_id", "value": "1564262492", "source": "CATDOG"},
],
"is_registered_in_vet_portal": True,
},
"name": "Great Western Exotics (Vets Now)",
"practice_types": [],
"corporate_network": "VetsNow",
}
) == {
"_id": "pra_2tV33hckuMLTKm9nXqnRbE",
"address.city": "Swindon",
"address.country": "UK",
"address.county": "Wiltshire",
"address.line_1": "Unit 10 Berkshire House",
"address.line_2": "",
"address.post_code": "SN1 2NR",
"corporate_network": "VetsNow",
"metadata.integration_ids[0].key": "vet_covea_id",
"metadata.integration_ids[0].source": "CATDOG",
"metadata.integration_ids[0].value": "127258",
"metadata.integration_ids[1].key": "vet_algolia_id",
"metadata.integration_ids[1].source": "CATDOG",
"metadata.integration_ids[1].value": "1564262492",
"metadata.is_registered_in_vet_portal": True,
"name": "Great Western Exotics (Vets Now)",
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment