Search code examples
jsonpython-3.xamazon-web-servicesamazon-snsboto3

JSON encoding error publishing SNS message with Boto 3


I am trying to send a simple JSON message to an Amazon SNS topic in Boto 3. However, I keep getting a _jsonparsefailure in the tag of the message and I only receive the default value. Here is my code:

    mess = {'default': 'default', 'this': 'that'}
    jmess = json.JSONEncoder().encode(mess)

    response = self.boto_client.publish(
        TopicArn = self.TopicArn,
        MessageStructure = 'json',
        Message = jmess
    )

I have also tried json.dumps(), which produces the same result.

    mess = {'default': 'default', 'this': 'that'}
    jmess = json.dumps(mess)

    response = self.boto_client.publish(
        TopicArn = self.TopicArn,
        MessageStructure = 'json',
        Message = jmess
    )

I seem to be following all of the guidelines set by the documentation, and I'm not getting an exception when I run the script. There are SQS queues that subscribe to the topic, and I am pulling the result data straight from the console.


Solution

  • 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.