I don't know why in this code the array returned the length is correct but is always the same result:
Expected result:
[{
'_uid': '1',
'total_amount': 1,
'employee_Email': 'one@users.com',
'employee_Full_name': 'full_name 1'
}, {
'_uid': '2',
'total_amount': 2,
'employee_Email': 'two@users.com',
'employee_Full_name': 'full_name 2'
}, {
'_uid': '3',
'total_amount': 3,
'employee_Email': 'three@users.com',
'employee_Full_name': 'full_name 3'
}]
output:
[{
'_uid': '3',
'total_amount': 3,
'employee_Email': 'three@users.com',
'employee_Full_name': 'full_name 3'
}, {
'_uid': '3',
'total_amount': 3,
'employee_Email': 'three@users.com',
'employee_Full_name': 'full_name 3'
}, {
'_uid': '3',
'total_amount': 3,
'employee_Email': 'three@users.com',
'employee_Full_name': 'full_name 3'
}]
Input data:
graphql_results = [
{
"_uid": "1",
"total_amount": 1,
"employee": {"Email": "one@users.com", "Full_name": "full_name 1"},
},
{
"_uid": "2",
"total_amount": 2,
"employee": {"Email": "two@users.com", "Full_name": "full_name 2"},
},
{
"_uid": "3",
"total_amount": 3,
"employee": {"Email": "three@users.com", "Full_name": "full_name 3"},
}
]
def normalize_json(
data: dict, key_string: str = "", normalized_dict: dict = {}, separator: str = "_"
) -> dict:
if isinstance(data, dict):
for key, value in data.items():
new_key = f"{key_string}{separator}{key}"
normalize_json(
data=value,
key_string=new_key
if new_key[: len(separator)] != separator
else new_key[len(separator) :],
normalized_dict=normalized_dict,
)
else:
normalized_dict[key_string] = data
return dict(normalized_dict)
normalize_json_array = [normalize_json(row) for row in graphql_results]