I have this data:
enquiry_metadata = {'enquiry_id': '02e19c4e21d2',
'items': [{'status': 'SUCCESS', 'code': 0, 'panelId': '0a1702be81bf', 'data': {'customerId': 19373, 'appId': 30531, 'zip': 80124, 'service_id': 979869}},
{'status': 'SUCCESS', 'code': 0, 'panelId': '6e638d5fbb', 'data': {'customerId': 30743, 'appId': 51808, 'zip': 32425, 'service_id': 879463}}]}
How can I add the key:value "companyId":"8424"
inside the list of dictionary enquiry_metadata['items']
, to get a result like so?
enquiry_metadata = {'enquiry_id': '02e19c4e21d2',
'items': [{'status': 'SUCCESS', 'code': 0, 'panelId': '0a1702be81bf', 'data': {'customerId': 19373, 'appId': 30531, 'zip': 80124, 'service_id': 979869}, 'companyId': '8424'},
{'status': 'SUCCESS', 'code': 0, 'panelId': '6e638d5fbb', 'data': {'customerId': 30743, 'appId': 51808, 'zip': 32425, 'service_id': 879463}, 'companyId': '8424'}]}
CodePudding user response:
You want to iterate the items array then set the key/value of each:
from pprint import pprint
enquiry_metadata = {
'enquiry_id': '02e19c4e21d2',
'items': [
{'status': 'SUCCESS', 'code': 0, 'panelId': '0a1702be81bf', 'data': {'customerId': 19373, 'appId': 30531, 'zip': 80124, 'service_id': 979869}},
{'status': 'SUCCESS', 'code': 0, 'panelId': '6e638d5fbb', 'data': {'customerId': 30743, 'appId': 51808, 'zip': 32425, 'service_id': 879463}}
]
}
for i in enquiry_metadata['items']:
i['companyId'] = '8424'
pprint(enquiry_metadata)
and here it resulting output:
{'enquiry_id': '02e19c4e21d2',
'items': [{'code': 0,
'companyId': '8424',
'data': {'appId': 30531,
'customerId': 19373,
'service_id': 979869,
'zip': 80124},
'panelId': '0a1702be81bf',
'status': 'SUCCESS'},
{'code': 0,
'companyId': '8424',
'data': {'appId': 51808,
'customerId': 30743,
'service_id': 879463,
'zip': 32425},
'panelId': '6e638d5fbb',
'status': 'SUCCESS'}]}
CodePudding user response:
enquiry_metadata = {
'enquiry_id': '02e19c4e21d2',
'items': [{
'status': 'SUCCESS',
'code': 0,
'panelId': '0a1702be81bf',
'data': {'customerId': 19373, 'appId': 30531, 'zip': 80124, 'service_id': 979869}
}, {
'status': 'SUCCESS',
'code': 0,
'panelId': '6e638d5fbb',
'data': {'customerId': 30743, 'appId': 51808, 'zip': 32425, 'service_id': 879463}
}
]}
for item in enquiry_metadata:
if "items" == item:
for itemsItem in enquiry_metadata[item]:
itemsItem["companyId"] = "8424"
print(enquiry_metadata)
output:
{
'enquiry_id': '02e19c4e21d2',
'items': [{
'status': 'SUCCESS',
'code': 0,
'panelId': '0a1702be81bf',
'data': {
'customerId': 19373,
'appId': 30531,
'zip': 80124,
'service_id': 979869
},
'companyId': '8424'
}, {
'status': 'SUCCESS',
'code': 0,
'panelId': '6e638d5fbb',
'data': {
'customerId': 30743,
'appId': 51808,
'zip': 32425,
'service_id': 879463
},
'companyId': '8424'
}]
}
CodePudding user response:
It can also be done like this
from pprint import pprint
enquiry_metadata = {
'enquiry_id': '02e19c4e21d2',
'items': [
{'status': 'SUCCESS', 'code': 0, 'panelId': '0a1702be81bf', 'data': {'customerId': 19373, 'appId': 30531, 'zip': 80124, 'service_id': 979869}},
{'status': 'SUCCESS', 'code': 0, 'panelId': '6e638d5fbb', 'data': {'customerId': 30743, 'appId': 51808, 'zip': 32425, 'service_id': 879463}}
]
}
for item in enquiry_metadata['items']:
item['companyId'] = '8424'
pprint(enquiry_metadata)
and the output will be
{'enquiry_id': '02e19c4e21d2',
'items': [{'code': 0,
'companyId': '8424','data': {'appId': 30531,
'customerId': 19373,
'service_id': 979869,
'zip': 80124},
'panelId': '0a1702be81bf',
'status': 'SUCCESS'},
{'code': 0,
'companyId': '8424',
'data': {'appId': 51808,
'customerId': 30743,
'service_id': 879463,
'zip': 32425},
'panelId': '6e638d5fbb',
'status': 'SUCCESS'}]}