0

I have a JSON with many nested dictionaries and list that I need to modify and when I want to find the data it returns no result. However it prints the expected values before the return.

Below a simplified structure of the JSON that I need to work with:

nested_dict = {'k': {'component':'media','value':'It is a test'}}

My code below:

def find_component(data: object, component: str):
    """
    """
    if isinstance(data, list):
        for i, k in enumerate(data):
            find_component(data[i], component)
    if isinstance(data, dict):
        for k, v in data.items():

            if k == 'component' and v == component:
                print(k, v)
                print('Find Component', data)
                return data
            else:
                find_component(data[k], component)
# Call to the recursive function
res = find_component(nested_dict, 'media')

The results of the print are the expected ones:

component media
Find Component {'component': 'media', 'value': 'It is a test'}

However the result of the data is None

TMikonos
  • 359
  • 1
  • 11
  • 29

3 Answers3

1

You need to return find_component. When you just call it, the result is lost, and the function returns None because that's what is implicitly returned by any function that does not explicitly return a value.

blue_note
  • 27,712
  • 9
  • 72
  • 90
1

You forgot a couple of returns:

def find_component(data: object, component: str):
    """
    """
    if isinstance(data, list):
        for i, k in enumerate(data):
            return find_component(data[i], component)
    if isinstance(data, dict):
        for k, v in data.items():
            if k == 'component' and v == component:
                print(k, v)
                print('Find Component', data)
                return data
            else:
                return find_component(data[k], component)
Joan Lara
  • 1,362
  • 8
  • 15
  • I am new to recursion, so basically I need to return the function in every instance that i don't want to get the results? – TMikonos Nov 11 '19 at 15:49
  • 1
    @TMikonos You need to return from every recursive call so the data can be passed back up to previous calls, or else the initial call will never receive the data. – Carcigenicate Nov 11 '19 at 15:50
  • 1
    Basically what @Carcigenicate said, you have to return something every time the function is called. – Joan Lara Nov 11 '19 at 15:52
  • It works with the sample data created for this but with the original JSON it stops in the first dictionary instance. It is weird. – TMikonos Nov 11 '19 at 16:16
0

Based on the answer from @Joan Lara, I have found a solution that it works. I needed to check if the result is not None to return the result of the function call, so the code becomes like this:

def find_component(data: object, component: str):
    """
    """
    if isinstance(data, list):
        for i, k in enumerate(data):

           res = find_component(data[i], component)
           if res is not None:
               return res
    if isinstance(data, dict):
        for k, v in data.items():

            if k == 'component' and v == component:
                print('I found the component', k, v)
                #print('Find Component', data)
                return data
            else:
                res = find_component(data[k], component)
                if res is not None:
                    return res
TMikonos
  • 359
  • 1
  • 11
  • 29