How to parse JSON dict and append the second nested portion to a list?

Viewed 29

The function below parses through an nested dictionary that is returned from an API call. (message_list is the call itself being passed in, complete_html is the list I am building) There are partId segments in the pass in that contain either a) the original text or b) HTML text. I want to call the HTML part, the second part, which is nested as {'partId': '1',

I think there is some very simple syntax, bracket or something of that sort in the function that needs to be added so that when attempting to append the nested partId: 1 messages to part_list, I grab all of the messages. Hoping that makes sense. What am I missing?

Please note I think that if I simply append 'mimeType': 'text/html', instead of appending the partId segments first, I am unable to loop through and append all of the passed in messages because partId needs to be appended first, before doing that.

[{'partId': '0', 'mimeType': 'text/plain', 'filename': '', 'headers': [{'name': 'Content-Type', 'value': 'text/plain; charset=UTF-8'}, {'name': 'Content-Transfer-Encoding', 'value': 
[{'partId': '0', 'mimeType': 'text/plain', 'filename': '', 'headers': [{'name': 'Content-Type', 'value': 'text/plain; charset=UTF-8'}, {'name': 'Content-Transfer-Encoding', 'value':



{'partId': '1', 'mimeType': 'text/html', 'filename': '', 'headers': [{'name': 'Content-Type', 'value': 'text/html; charset=UTF-8'}, {'name': 'Content-Transfer-Encoding', 'value': 
{'partId': '1', 'mimeType': 'text/html', 'filename': '', 'headers': [{'name': 'Content-Type', 'value': 'text/html; charset=UTF-8'}, {'name': 'Content-Transfer-Encoding', 'value':
def return_complete_html(message_list, complete_list):
    for msg_ in message_list:
        msg_id = msg_['id']
        msg = service.users().messages().get(userId='me', id=msg_id, format='full').execute()
        payload = msg['payload']['parts']

        part_list = []
        
        for part_html in payload:
            
            # mimeType = part_html.get("mimeType")
            # if mimeType == "text/html":
            #     part_list.append(part_html)
            
            part_ID = part_html.get("partId")
            part_list.append(part_html)
1 Answers

Somewhere, try this to get to the second nest:

part_id = part.get("partId")
            if part_id == "1":
Related