Nested_Dict =
{'candela_samples_generic': {'drc_dtcs': {'domain_name': 'TEMPLATE-DOMAIN', 'dtc_all':{
'0x930001': {'identification': {'udsDtcValue': '0x9300', 'FaultType': '0x11', 'description': 'GNSS antenna short to ground'},
'functional_conditions': {'failure_name': 'short_to_ground', 'mnemonic': 'DTC_GNSS_Antenna_Short_to_ground'}},
'0x212021': {'identification': {'udsDtcValue': '0x2120', 'FaultType': '0x21', 'description': 'ECU internal Failure'},
'functional_conditions': {'failure_name': 'short_to_ground', 'mnemonic': 'DTC_GNSS_Antenna_Short_to_ground'}}}}}}
Header = {
'dtc_all': {
'DiagnosticTroubleCodeUds': {'udsDtcValue': None, 'FaultType': None},
'dtcProps': {'description': None},
'DiagnosticTroubleCodeObd': {'failure_name': None}
}
}
SubkeyList = ['0x930001','0x240001']
Take an element from the SubkeyList and one header keys at a time from the dictionary (there can be multiple header keys like dtc_all). Iterate inside the header dictionary over the values of the dictionary, such as 'udsDtcValue'.
For example:
Main_Key = dtc_all
Sub_Key = 0x212021
Element = udsDtcValue
Pass these parameters to the function get_value_nested_dict(nested_dict, Main_Key, Sub_Key, Element). This function will return the element value. get_value_nested_dict func which is working as expected for Element value retriaval I've posted for the reference. At the same time, create a new dictionary and update the element value at the right place, such as 'udsDtcValue': '0x9300'. Also, ensure that the sequence of keys remains the same as in the header. Similarly, iterate inside the header dictionary over all the values of the dictionary, such as FaultType, description, until failure_name. Repeat these iterations for each element in the SubkeyList and append the results in the new_dict in the same sequence. Any suggestions on how to proceed?
Output: New_Dict should look like this:
New_Dict=
{'dtc_all':
{'0x930001': {'DiagnosticTroubleCodeUds': {'udsDtcValue': '0x9300', 'FaultType': '0x11'}, 'dtcProps':{'description': 'GNSS antenna short to ground'}, 'DiagnosticTroubleCodeObd': {'failure_name':short_to_ground}}},
{'0x212021': {'DiagnosticTroubleCodeUds': {'udsDtcValue': '0x2120', 'FaultType': '0x21'}, 'dtcProps':{'description': 'ECU internal Failure'}, 'DiagnosticTroubleCodeObd': {'failure_name':short_to_ground}}}}
def get_value_nested_dict(nested_dict, main_key, sub_key, element):
results = []
def search_for_element(sub_dict, looking_for_element):
if isinstance(sub_dict, dict):
for k, v in sub_dict.items():
if k == element and looking_for_element:
results.append(v)
else:
search_for_element(v, looking_for_element)
def search_nested_dict(current_dict):
if isinstance(current_dict, dict):
for key, value in current_dict.items():
if key == main_key:
if sub_key in value:
search_for_element(value[sub_key], True)
else:
search_for_element(value, False)
else:
search_nested_dict(value)
search_nested_dict(nested_dict)
return results
Initialize an empty dictionary new_dict
Iterate over the SubkeyList
For each sub_key
in SubkeyList
create a new dictionary: sub_dict
For each element
in the Header['dtc_all']
get the value from the Nested_Dict
using get_value_nested_dict
and add it to sub_dict
Add sub_dict
to new_dict
under sub_key
def create_new_dict(Nested_Dict, Header, SubkeyList):
new_dict = {}
for sub_key in SubkeyList:
sub_dict = {}
for element, value in Header['dtc_all'].items():
value = get_value_nested_dict(Nested_Dict, 'dtc_all', sub_key, element)
if value:
sub_dict[element] = value[0]
new_dict[sub_key] = sub_dict
return new_dict
New_dict = create_new_dict(Nested_Dict, Header, SubkeyList)