0

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!

1 Answer 1

2

myDesc = [(data['emailList'][0]['emailTypeDesc'])]

You're taking the 'emailTypeDesc' of the first 'emailList' and iterating over it.

What i think you need is myDesc = [mails['emailTypeDesc'] for mails in data['emailList']] and then iterate over it.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot Kay! this solved my issue. However, would you know how could I add requestId from my emailList in front of the results in final_list? Now I managed to get final_list=['Email-In', 'Email-Out', 'Email-In']. I would need requestId to show in front of this with no repetition, so final_list= [12345667, 'Email-In', 'Email-Out', 'Email-In'].
Nevermind, I added something like this and worked: myId = [(data['emailList'][0]['requestId'])] for each_req in myId: final_list.append(each_req)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.