1

Getting TypeError: string indices must be integers on the my_object instantiation here below (3rd line of method)

def get_note_retrieval_body(event):
    sns_body = event["Records"][0]["Sns"]
    message = json.loads(sns_body["Message"])
    my_object = message["data"]["getNotes"]
    return my_object

I get the following (account numbers etc are marked with XXX) when i use str(event) and then json format it, so this will tell you what the event looks like

{
    'Records': [{
        'EventSource': 'aws:sns',
        'EventVersion': '1.0',
        'EventSubscriptionArn': 'arn:aws:sns:us-east-1:XXXXXX:topic-name-sandbox:7XXXX',
        'Sns': {
            'Type': 'Notification',
            'MessageId': 'd1074c88-ae21-52b6-8a75-1b07d766cfdd',
            'TopicArn': 'arn:aws:sns:us-east-1:XXXXXXX:topic-name',
            'Subject': None,
            'Message': '"{\\"data\\": {\\"getNotes\\": {\\"claimNumber\\": \\"AAAB09000010\\", \\"dateEntered\\": \\"2010-04-22T08:03:53\\",\\"categoryCode\\": \\"fdf49\\",\\"subCategoryCode\\": \\"ATT\\", \\"fileNoteTextDetails\\": [{\\"fileNoteText\\": {\\"fileNoteID\\": \\"112B40FE42934055\\", \\"noteText\\": \\"Send Acknowledgement Letter to Claimant\\", \\"authorID\\": \\"0\\"}, \\"fileNoteAttachments\\": [{\\"attachment\\": {\\"fileName\\": \\"F70F880879D35FC4.doc\\", \\"fileExtension\\": \\".URL\\", \\"dateCreated\\": \\"2010-04-22T08:59:57\\", \\"createdBy\\": \\"CLONER\\", \\"dateUpdated\\": \\"2020-07-30T08:36:19.1903051\\", \\"updatedBy\\": \\"EVERYONE\\"}}]}], \\"fileNoteExtendedEntityData\\": {\\"dateOnDocument\\": \\"2010-04-22T08:59:57\\", \\"serviceDateFrom\\": \\"2010-04-22T08:59:57\\", \\"serviceDateThrough\\": \\"2010-04-22T08:59:57\\", \\"author\\": \\"n0000000\\"}}}}"',
            'Timestamp': '2020-07-20T10:50:47.850Z',
            'SignatureVersion': '1',
            'Signature': 'XXXXXX',
            'SigningCertUrl': 'https://sns.us-east-1.amazonaws.com/XXXcert.pem',
            'UnsubscribeUrl': 'https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:XXXXXX:topic-name-sandbox:7XX'.
            'MessageAttributes': {}
        }
    }]
}

1 Answer 1

2

When you run json.loads on sns_body["Message"], you still get back a string. You can run json.loads twice, and that should solve the issue.

sns_body["Message"] is quoted twice (i.e., it is wrapped in single quotes and double quotes). So when you try to decode it once, you still get back a string, but this time it is only quoted once. Then a second json.loads will decode the string into a dictionary.

def get_note_retrieval_body(event):
    sns_body = event["Records"][0]["Sns"]
    message = json.loads(json.loads(sns_body["Message"]))
    my_object = message["data"]["getNotes"]
    return my_object
Sign up to request clarification or add additional context in comments.

3 Comments

I don't want to change the code, this is something I am picking up after someone else and I want to write a test, so would be more interested in changing the JSON payload.
You didn't mention that in your question. In that case, change the string to whatever json.loads outputs.
I know yea only thinking of it now, I'll be at laptop tomorrow if this works I'll accept the answer

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.