0

I'm trying to get two attributes at the time from my json data and add them as an item on my python list. However, when trying to add those two: ['emailTypeDesc']['createdDate'] it throws an error. Could someone help with this? thanks in advance!

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:

final_list = []

data = json.loads(r.text)
myId = [(data['emailList'][0]['requestId'])]
for each_req in myId:
    final_list.append(each_req)
myEmailList = [mails['emailTypeDesc']['createdDate'] for mails in data['emailList']]
for each_requ in myEmailList:
    final_list.append(each_requ)
return final_list

This error comes up when I run the above code:

TypeError: string indices must be integers

Desired output for final_list:

[12345667, 'Email-In', '15-May-2020 11:15:52',  'Email-Out', '16-May-2020 16:15:52', 'Email-In', '17-May-2020 20:15:52']

My problem is definetely in this line:

myEmailList = [mails['emailTypeDesc']['createdDate'] for mails in data['emailList']]

because when I run this without the second attribute ['createdDate'] it would work, but I need both attributes on my final_list:

  myEmailList = [mails['emailTypeDesc'] for mails in data['emailList']]

2 Answers 2

1

I think you're misunderstanding the syntax. mails['emailTypeDesc']['createdDate'] is looking for the key 'createdDate' inside the object mails['emailTypeDesc'], but in fact they are two items at the same level.

Since mails['emailTypeDesc'] is a string, not a dictionary, you get the error you have quoted. It seems that you want to add the two items mails['emailTypeDesc'] and mails['createdDate'] to your list. I'm not sure if you'd rather join these together into a single string or create a sub-list or something else. Here's a sublist option.

myEmailList = [[mails['emailTypeDesc'], mails['createdDate']] for mails in data['emailList']]
Sign up to request clarification or add additional context in comments.

Comments

0

Strings in JSON must be in double quotes, not single.

Edit: As well as names.

Comments

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.