how do you create an automatic mapping of possible json data options to be collected?

If the elements in the events arrays are the same, this code works without errors.

def get_prints(recode: dict):
    for key in recode.keys():
        if type(recode[key]) == dict:
            for sub_print in get_prints(recode[key]):
                yield [key] + sub_print
        else:
            yield [key]



class Automater:
    def __init__(self,name: str):
        """
        Params:
            name: name of json
        """
        self.name = name

    def get_print(self,*args):
        """
        Params:
            *args: keys json
        """
        return '_'.join(args) + ' = ' + self.name + ''.join([f"['{arg}']" for arg in args])

For example, this code:

dicts = {
    'tournament':{
        'name':"any name",
        'slug':'somthing else',
        'sport':{
            'name':'sport',
            'anotherdict':{
                'yes':True
            }
        }
    }
}
list_names = get_prints(dicts)
for name in list_names:
    print(auto.get_print(*name))

Gives this output:

tournament_name = event['tournament']['name']
tournament_slug = event['tournament']['slug']
tournament_sport_name = event['tournament']['sport']['name']
tournament_sport_anotherdict_yes = event['tournament']['sport']['anotherdict']['yes']

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top