takanoha
takanoha

Reputation: 353

How to read body of the sqs message on AWS

I am writing a script in python to receive Amazon SQS messages.

In the Amazon SQS Queue I have such message as

'{"Test":"Machine-name", "State":"Request", "Description":""}'

In the python script I can receive this message; however, it complains as following:

msg = json.loads(message)
  File "/usr/lib/python3.8/json/__init__.py", line 341, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not sqs.Message

I believe the message I am inserting to the queue is in json format, but why does it recognize as sqs.Message ?

How do I convert to json format ?

Thanks,

Upvotes: 1

Views: 6820

Answers (1)

httpdigest
httpdigest

Reputation: 5797

When you read an SQS message (or rather a list of SQS messages) via Boto3 (the Python AWS SDK) you are not given the payload or body of a single message directly, but the dict containing a list of messages where each message includes other attributes like MessageId.

As part of a message (in the list of messages) you have the Body attribute, which is what you want.

See: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.receive_message

Upvotes: 1

Related Questions