JSON encoding error publishing SNS message with Boto 3 JSON encoding error publishing SNS message with Boto 3 python-3.x python-3.x

JSON encoding error publishing SNS message with Boto 3


It turns out the message needs to look like this:

json.dumps({"default": "my default", "sqs": json.dumps({"this": "that"})})

Amazon has horrible documentation in this regard.

You can also remove the MessageStructure='json'and send just json.dumps({'this':'that'}) if you set the SQS queue to receive just the raw message. This is simply done through the console.


This is how I fixed it:

message = {"record_id": "my_id", "name": "value"}json_message = json.dumps({"default":json.dumps(message)})sns_client.publish("topic_arn", Subject="test", MessageStructure="json", Message=json_message)

SNS expects "default" as the key which contains the message to be published.


In Boto 3 (I'm using v1.4.7) this is the format:

sns.publish(TopicArn="topic_arn", Message=json.dumps({"this": "that"},ensure_ascii=False))

There isn't any need for the protocol definition, i.e. "default" unless you are delivering different structures per protocol, i.e., JSON for Lambda and HTML for email.