title: Python how to return a dictionary from a recursive function parsing a nested json?
body: <p>I'm trying to parse a nested json file for a specific key value &quot;Date&quot; and return a dictionary that should look like</p>
  <pre><code>{'Date', '2022-05-20T00:00:00Z'}
  </code></pre>
  <p>Here is my code:</p>
  <pre><code>meetingDate_dict = {}
  def recursive_json(data,attr,m_dict):
      for k,v in data.items():
          if k == attr:
              for k2,v2 in v.items():
                  m_dict = {attr, v2}
                  print('IF: ',m_dict)
                  return m_dict
          elif isinstance(v,dict):
              print('ELIF: ', k,v)
              return recursive_json(v,attr,m_dict)
          else:
              print('ELSE: ',k,v)
  
      print('END')
  
  print('RETURN: ',recursive_json(assessment_json, &quot;Date&quot;, meetingDate_dict))
  </code></pre>
  <p>Output:</p>
  <pre><code>ELSE:  $id 1
  ELIF:  DataChangedEntry {'$id': '2', 'PathProperty': '/', 'Metadata': None, 'PreviousValue': None, 'CurrentValue': {'CosewicWsRefId': {'Value': 'QkNlrjq2HL9bhTQqU8-qH'}, 'Date': {'Value': '2022-05-20T00:00:00Z'}, 'YearSentToMinister': {'Value': '0001-01-01T00:00:00'}, 'DateSentToMinister': {'Value': '0001-01-01T00:00:00'}, 'Order': None, 'Type': {'Value': 'REGULAR'}, 'ReportType': {'Value': 'NEW'}, 'Stage': {'Value': 'ASSESSED'}, 'State': {'Value': 'PUBLISHED'}, 'StatusAndCriteria': {'Status': {'Value': 'EXTINCT'}, 'StatusComment': {'EnglishText': None, 'FrenchText': None}, 'StatusChange': {'Value': 'NOT_INITIALIZED'}, 'StatusCriteria': {'EnglishText': None, 'FrenchText': None}, 'ApplicabilityOfCriteria': {'ApplicabilityCriteriaList': []}}, 'Designation': None, 'Note': None, 'DomainEvents': [], 'Version': {'Value': 1651756761385.1248}, 'Id': {'Value': '3z3XlCkaXY9xinAbK5PrU'}, 'CreatedAt': {'Value': 1651756761384}, 'ModifiedAt': {'Value': 1651756785274}, 'CreatedBy': {'Value': 'Geordie.Powers@ec.gc.ca'}, 'ModifiedBy': {'Value': 'Geordie.Powers@ec.gc.ca'}}}
  ELSE:  $id 2
  ELSE:  PathProperty /
  ELSE:  Metadata None
  ELSE:  PreviousValue None
  ELIF:  CurrentValue {'CosewicWsRefId': {'Value': 'QkNlrjq2HL9bhTQqU8-qH'}, 'Date': {'Value': '2022-05-20T00:00:00Z'}, 'YearSentToMinister': {'Value': '0001-01-01T00:00:00'}, 'DateSentToMinister': {'Value': '0001-01-01T00:00:00'}, 'Order': None, 'Type': {'Value': 'REGULAR'}, 'ReportType': {'Value': 'NEW'}, 'Stage': {'Value': 'ASSESSED'}, 'State': {'Value': 'PUBLISHED'}, 'StatusAndCriteria': {'Status': {'Value': 'EXTINCT'}, 'StatusComment': {'EnglishText': None, 'FrenchText': None}, 'StatusChange': {'Value': 'NOT_INITIALIZED'}, 'StatusCriteria': {'EnglishText': None, 'FrenchText': None}, 'ApplicabilityOfCriteria': {'ApplicabilityCriteriaList': []}}, 'Designation': None, 'Note': None, 'DomainEvents': [], 'Version': {'Value': 1651756761385.1248}, 'Id': {'Value': '3z3XlCkaXY9xinAbK5PrU'}, 'CreatedAt': {'Value': 1651756761384}, 'ModifiedAt': {'Value': 1651756785274}, 'CreatedBy': {'Value': 'Geordie.Powers@ec.gc.ca'}, 'ModifiedBy': {'Value': 'Geordie.Powers@ec.gc.ca'}}
  ELIF:  CosewicWsRefId {'Value': 'QkNlrjq2HL9bhTQqU8-qH'}
  ELSE:  Value QkNlrjq2HL9bhTQqU8-qH
  END
  RETURN:  None
  </code></pre>
  <p>So this way doesn't ever print the correct key:value pair, and returns None.
  If I remove the <code>return</code> statement in the <code>elif</code> I at least get the correct print out from the <code>if</code> statement, but I still get <code>None</code> returned:</p>
  <pre><code>meetingDate_dict = {}
  def recursive_json(data,attr,m_dict):
      for k,v in data.items():
          if k == attr:
              for k2,v2 in v.items():
                  m_dict = {attr, v2}
                  print('IF: ',m_dict)
                  return m_dict
          elif isinstance(v,dict):
              print('ELIF: ', k,v)
              recursive_json(v,attr,m_dict)
          else:
              print('ELSE: ',k,v)
  
      print('END')
  
  print('RETURN: ',recursive_json(assessment_json, &quot;Date&quot;, meetingDate_dict))
  </code></pre>
  <p>Output:</p>
  <pre><code>ELSE:  $id 1
  ELIF:  DataChangedEntry {'$id': '2', 'PathProperty': '/', 'Metadata': None, 'PreviousValue': None, 'CurrentValue': {'CosewicWsRefId': {'Value': 'QkNlrjq2HL9bhTQqU8-qH'}, 'Date': {'Value': '2022-05-20T00:00:00Z'}, 'YearSentToMinister': {'Value': '0001-01-01T00:00:00'}, 'DateSentToMinister': {'Value': '0001-01-01T00:00:00'}, 'Order': None, 'Type': {'Value': 'REGULAR'}, 'ReportType': {'Value': 'NEW'}, 'Stage': {'Value': 'ASSESSED'}, 'State': {'Value': 'PUBLISHED'}, 'StatusAndCriteria': {'Status': {'Value': 'EXTINCT'}, 'StatusComment': {'EnglishText': None, 'FrenchText': None}, 'StatusChange': {'Value': 'NOT_INITIALIZED'}, 'StatusCriteria': {'EnglishText': None, 'FrenchText': None}, 'ApplicabilityOfCriteria': {'ApplicabilityCriteriaList': []}}, 'Designation': None, 'Note': None, 'DomainEvents': [], 'Version': {'Value': 1651756761385.1248}, 'Id': {'Value': '3z3XlCkaXY9xinAbK5PrU'}, 'CreatedAt': {'Value': 1651756761384}, 'ModifiedAt': {'Value': 1651756785274}, 'CreatedBy': {'Value': 'Geordie.Powers@ec.gc.ca'}, 'ModifiedBy': {'Value': 'Geordie.Powers@ec.gc.ca'}}}
  ELSE:  $id 2
  ELSE:  PathProperty /
  ELSE:  Metadata None
  ELSE:  PreviousValue None
  ELIF:  CurrentValue {'CosewicWsRefId': {'Value': 'QkNlrjq2HL9bhTQqU8-qH'}, 'Date': {'Value': '2022-05-20T00:00:00Z'}, 'YearSentToMinister': {'Value': '0001-01-01T00:00:00'}, 'DateSentToMinister': {'Value': '0001-01-01T00:00:00'}, 'Order': None, 'Type': {'Value': 'REGULAR'}, 'ReportType': {'Value': 'NEW'}, 'Stage': {'Value': 'ASSESSED'}, 'State': {'Value': 'PUBLISHED'}, 'StatusAndCriteria': {'Status': {'Value': 'EXTINCT'}, 'StatusComment': {'EnglishText': None, 'FrenchText': None}, 'StatusChange': {'Value': 'NOT_INITIALIZED'}, 'StatusCriteria': {'EnglishText': None, 'FrenchText': None}, 'ApplicabilityOfCriteria': {'ApplicabilityCriteriaList': []}}, 'Designation': None, 'Note': None, 'DomainEvents': [], 'Version': {'Value': 1651756761385.1248}, 'Id': {'Value': '3z3XlCkaXY9xinAbK5PrU'}, 'CreatedAt': {'Value': 1651756761384}, 'ModifiedAt': {'Value': 1651756785274}, 'CreatedBy': {'Value': 'Geordie.Powers@ec.gc.ca'}, 'ModifiedBy': {'Value': 'Geordie.Powers@ec.gc.ca'}}
  ELIF:  CosewicWsRefId {'Value': 'QkNlrjq2HL9bhTQqU8-qH'}
  ELSE:  Value QkNlrjq2HL9bhTQqU8-qH
  END
  IF:  {'Date', '2022-05-20T00:00:00Z'}
  END
  ELSE:  EventAction Create
  ELIF:  EventDataChange {'$ref': '2'}
  ELSE:  $ref 2
  END
  ELSE:  CorrelationId 3z3XlCkaXY9xinAbK5PrU
  ELSE:  EventId WGxlewsUAHayLHZ2LHvFk
  ELSE:  EventTimeUtc 2022-05-06T13:15:31.7463355Z
  ELSE:  EventDataVersion 1.0.0
  ELSE:  EventType AssessmentCreatedInfrastructure
  END
  RETURN:  None
  </code></pre>
  <p>The json file:</p>
  <pre><code>{
    &quot;$id&quot;: &quot;1&quot;,
    &quot;DataChangedEntry&quot;: {
      &quot;$id&quot;: &quot;2&quot;,
      &quot;PathProperty&quot;: &quot;/&quot;,
      &quot;Metadata&quot;: null,
      &quot;PreviousValue&quot;: null,
      &quot;CurrentValue&quot;: {
        &quot;CosewicWsRefId&quot;: {
          &quot;Value&quot;: &quot;QkNlrjq2HL9bhTQqU8-qH&quot;
        },
        &quot;Date&quot;: {
          &quot;Value&quot;: &quot;2022-05-20T00:00:00Z&quot;
        },
        &quot;YearSentToMinister&quot;: {
          &quot;Value&quot;: &quot;0001-01-01T00:00:00&quot;
        },
        &quot;DateSentToMinister&quot;: {
          &quot;Value&quot;: &quot;0001-01-01T00:00:00&quot;
        },
        &quot;Order&quot;: null,
        &quot;Type&quot;: {
          &quot;Value&quot;: &quot;REGULAR&quot;
        },
        &quot;ReportType&quot;: {
          &quot;Value&quot;: &quot;NEW&quot;
        },
        &quot;Stage&quot;: {
          &quot;Value&quot;: &quot;ASSESSED&quot;
        },
        &quot;State&quot;: {
          &quot;Value&quot;: &quot;PUBLISHED&quot;
        },
        &quot;StatusAndCriteria&quot;: {
          &quot;Status&quot;: {
            &quot;Value&quot;: &quot;EXTINCT&quot;
          },
          &quot;StatusComment&quot;: {
            &quot;EnglishText&quot;: null,
            &quot;FrenchText&quot;: null
          },
          &quot;StatusChange&quot;: {
            &quot;Value&quot;: &quot;NOT_INITIALIZED&quot;
          },
          &quot;StatusCriteria&quot;: {
            &quot;EnglishText&quot;: null,
            &quot;FrenchText&quot;: null
          },
          &quot;ApplicabilityOfCriteria&quot;: {
            &quot;ApplicabilityCriteriaList&quot;: []
          }
        },
        &quot;Designation&quot;: null,
        &quot;Note&quot;: null,
        &quot;DomainEvents&quot;: [],
        &quot;Version&quot;: {
          &quot;Value&quot;: 1651756761385.1248
        },
        &quot;Id&quot;: {
          &quot;Value&quot;: &quot;3z3XlCkaXY9xinAbK5PrU&quot;
        },
        &quot;CreatedAt&quot;: {
          &quot;Value&quot;: 1651756761384
        },
        &quot;ModifiedAt&quot;: {
          &quot;Value&quot;: 1651756785274
        },
        &quot;CreatedBy&quot;: {
          &quot;Value&quot;: &quot;a@a&quot;
        },
        &quot;ModifiedBy&quot;: {
          &quot;Value&quot;: &quot;a@a&quot;
        }
      }
    },
    &quot;EventAction&quot;: &quot;Create&quot;,
    &quot;EventDataChange&quot;: {
      &quot;$ref&quot;: &quot;2&quot;
    },
    &quot;CorrelationId&quot;: &quot;3z3XlCkaXY9xinAbK5PrU&quot;,
    &quot;EventId&quot;: &quot;WGxlewsUAHayLHZ2LHvFk&quot;,
    &quot;EventTimeUtc&quot;: &quot;2022-05-06T13:15:31.7463355Z&quot;,
    &quot;EventDataVersion&quot;: &quot;1.0.0&quot;,
    &quot;EventType&quot;: &quot;AssessmentCreatedInfrastructure&quot;
  }
  </code></pre>