I have json code from which I need to get requestId and emailTypeDesc that are inside emailList. This action has to be perform for each item on emailRequest list, see below:
json:
{
'readOnly': False,
'senderDetails': {'firstName': 'John', 'lastName': 'Doe', 'emailAddress': '[email protected]', 'emailAddressId': 123456, 'personalId': 123, 'companyName': 'ACME‘},
'clientDetails': {'firstName': 'Jane', 'lastName': 'Doe', 'emailAddress': '[email protected]', 'emailAddressId': 654321, 'personalId': 456, 'companyName': 'Lorem Ipsum‘}},
'notesSection': {},
'emailList': [{'requestId': 12345667, 'emailId': 9876543211, 'emailType': 3, 'emailTypeDesc': 'Email-In', 'emailTitle': 'SampleTitle 1', 'createdDate': '15-May-2020 11:15:52', 'fromMailList': [{'firstName': 'Jane', 'lastName': 'Doe', 'emailAddress': '[email protected]',}]},
{'requestId': 12345667, 'emailId': 14567775, 'emailType': 3, 'emailTypeDesc': 'Email-Out', 'emailTitle': 'SampleTitle 2', 'createdDate': '16-May-2020 16:15:52', 'fromMailList': [{'firstName': 'Jane', 'lastName': 'Doe', 'emailAddress': '[email protected]',}]},
{'requestId': 12345667, 'emailId': 12345, 'emailType': 3, 'emailTypeDesc': 'Email-In', 'emailTitle': 'SampleTitle 3', 'createdDate': '17-May-2020 20:15:52', 'fromMailList': [{'firstName': 'Jane', 'lastName': 'Doe', 'emailAddress': '[email protected]',}]
}
Python code:
emailRequest=['12345667']
final_list = []
def get_emailRequest_details(emailRequest, tokenId, userId):
headers = { 'accept': 'application/json',
'Content-Type': 'application/json',
'access-token': tokenId,
'userID': userId}
endpoint = 'emailRequest/' + x + '/details'
r = requests.get(url = HOSTNAME + endpoint, headers = headers, verify=False)
data = json.loads(r.text)
myDesc = [(data['emailList'][0]['emailTypeDesc'])]
for each_requ in myDesc:
final_list.append(each_requ)
return final_list
for x in emailRequest:
get_emailRequest_details(x, tokenId, userId)
However, when running the above code I can't get all values for emailTypeDesc (expected 3 list items, but code returns only latest). Also, not sure how I could get both attributes at the same time (requestId, emailTypeDesc) and add requestId on the front of my final_list.
Current output for final_list:
['Email-In']
Desired output for final_list:
[12345667, 'Email-In', 'Email-Out', 'Email-In']
Please note that this code will be looping through unknown number of email requests on this list: emailRequest, it can be one request at the time like in this example or many depending on the time this code will run.
So for multiple emailRequest my final_list output would look like:
[12345667, 'Email-In', 'Email-Out', 'Email-In', 987654, 'Email-In', 'Email-Out', 567843, 'Email-In', 'Email-Out', 'Email-In', 'Email-Out', 'Email-In', 'Email-In']
I guess I'm doing something wrong here:
myDesc = [(data['emailList'][0]['emailTypeDesc'])]
for each_requ in myDesc:
final_list.append(each_requ)
return final_list
Could someone help me with this? Thanks in advance!