2

How do I pull out the key in a nested dictionary of unknown length and equate that key to the traversal path and store them as a key value pair in another dictionary. I have a nested dictionary as below

{
    "ingestion_config": {
        "location": {},
        "start_sequence": {},
        "datafeed": {
            "t04047": {
                "validation": {
                    "triple_check": {},
                    "record_count_validation": {}
                },
                "date_pattern": {},
                "cdc_config": {}
            }
        }
    }
}

I am looking to fetch the keys at various levels and equate it the traversal path as below

{
ingestion_config: [ingestion_config]
location: [ingestion_config][location],
start_sequence: [ingestion_config][start_sequence],
datafeed: [ingestion_config][datafeed]
t04047: [ingestion_config][datafeed][t04047]
triple_check: [ingestion_config][data_feed][t04047][validation][trip_check]
}

The closest post I have found for a scenario similar to mine is: here

Balajee Addanki
  • 690
  • 2
  • 9
  • 23
  • 1
    Possible duplicate of [Extracting the keys associated in previous levels nested dictionary](https://stackoverflow.com/questions/53439528/extracting-the-keys-associated-in-previous-levels-nested-dictionary) – anjaneyulubatta505 Feb 13 '19 at 05:08

1 Answers1

1

You can use recursion with a generator. In the signature of the function, a default parameter is kept to track the paths accumulated by the recursion via the list concatenation at each call of paths:

d = {'ingestion_config': {'location': {}, 'start_sequence': {}, 'datafeed': {'t04047': {'validation': {'triple_check': {}, 'record_count_validation': {}}, 'date_pattern': {}, 'cdc_config': {}}}}}
def paths(_d, _c = []):
  for a, b in _d.items():
    yield _c+[a]
    yield from paths(b, _c+[a])

results = {i[-1]:''.join(f'[{a}]' for a in i) for i in paths(d)}

Output:

{'ingestion_config': '[ingestion_config]', 'location': '[ingestion_config][location]', 'start_sequence': '[ingestion_config][start_sequence]', 'datafeed': '[ingestion_config][datafeed]', 't04047': '[ingestion_config][datafeed][t04047]', 'validation': '[ingestion_config][datafeed][t04047][validation]', 'triple_check': '[ingestion_config][datafeed][t04047][validation][triple_check]', 'record_count_validation': '[ingestion_config][datafeed][t04047][validation][record_count_validation]', 'date_pattern': '[ingestion_config][datafeed][t04047][date_pattern]', 'cdc_config': '[ingestion_config][datafeed][t04047][cdc_config]'}
Ajax1234
  • 69,937
  • 8
  • 61
  • 102