Home > Software engineering >  Extract fields in a dynamically nested dictionary in an ordered manner
Extract fields in a dynamically nested dictionary in an ordered manner

Time:04-10

given the following dictionary - which is dynamically so there could be any level of deepness (nested structure):

data = {
    "level": {
        "system": {
            "text": "A Lorem ipsum colour."
        },
        "board": {
            "core": {
                "text": "Another one."
            }
        }
    },
    "peer": {
        "core": {
            "text": "B Lorem ipsum colour."
        },
        "main": {
            "text": "C Lorem ipsum colour."
        }
    }
}

The goal is to extract the text elements orderd (from top to bottom), the result should be something like:

result = "A Lorem ipsum colour. Another one. B Lorem ipsum colour. C Lorem ipsum colour.

I think I've to use some type of recursion but I can't get it. What I get so far - and what is not working - is the following:

for k, v in data.items():
    if isinstance(v, dict):
        # i think i have to continue here on a deeper recursion level
    else:
        return v["text"]

Best regards

CodePudding user response:

def dict_value(val: dict):
    for key, item in val.items():
        if type(item) == dict:
            dict_value(item)
        else:
            print(item)


dict_value(data)

CodePudding user response:

In order to get the result as stated in the original question:

data = {
    "level": {
        "system": {
            "text": "A Lorem ipsum colour."
        },
        "board": {
            "core": {
                "text": "Another one."
            }
        }
    },
    "peer": {
        "core": {
            "text": "B Lorem ipsum colour."
        },
        "main": {
            "text": "C Lorem ipsum colour."
        }
    }
}

def get_text(d, lst):
    for k, v in d.items():
        if isinstance(v, dict):
            get_text(v, lst)
        elif k == 'text':
            lst.append(v)
    return lst


print(' '.join(get_text(data, [])))

Output:

A Lorem ipsum colour. Another one. B Lorem ipsum colour. C Lorem ipsum colour.
  • Related