I am trying to update the key name in dictionary using python.
I tried a for loop like below, but it was basically overriding the values and not saving the values in a list format.
How can I recursively add it using a for loop, or is there any better approach for it?
for lstr_document_object in pdct_input_body["Input"]:
lstr_document_object["DocumentType"] = lstr_document_object["ObjectType"]
lstr_document_object["DocumentFormat"] = lstr_document_object["ObjectFormat"]
lstr_document_object["DocumentSourceFileName"] =
lstr_document_object["ObjectSourceFileName"]
- For directly updating the value I tried using, but it resulted in no updation.
sample_dict['DocumentType'] = sample_dict.pop('ObjectType')
- Sample Input
{
"Input": [
{
"ObjectType": "Document",
"ObjectFormat": "pdf",
"ObjectSourceFileName": "Mr.Dave_ICICI.pdf"
},
{
"ObjectType": "Document",
"ObjectFormat": "pdf",
"ObjectSourceFileName": "Mr.Dave_HDFC.pdf"
}
],
"Source": "Client",
"ClientCode": "CL"
}
- Desired Output
{
"Input": [
{
"DocumentType": "Document",
"DocumentFormat": "pdf",
"DocumentSourceFileName": "Mr.Dave_ICICI.pdf",
"UUID": "<generated-uuid>"
},
{
"DocumentType": "Document",
"DocumentFormat": "pdf",
"DocumentSourceFileName": "Mr.Dave_HDFC.pdf",
"UUID": "<generated-uuid>"
}
],
"Source": "Client",
"ClientCode": "CL"
}