JSON coding error for posting sns message using boto3

I am trying to send a simple JSON message to an sns topic in boto3. However, I keep getting _jsonparsefailure in the message tag, and I only get 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 also tried json.dumps (), which gives 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 follow all the guidelines set in the documentation, and I don't get an exception when I run the script. There are SQS queues that subscribe to this topic, and I retrieve the result data directly from the console.

+4
source share
3 answers

Ok, I decided. It turns out the message should look like this:

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

Amazon . , .

MessageStructure='json' json.dumps({'this':'that'}), sqs . .

+4

:

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 " " , .

+5

In boto3 (I am using v1.4.7) this is the format:

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

There is no need to define a protocol, i.e. "default" if you do not send different structures for the protocol, that is, json for Lambda and HTML for email

0
source

Source: https://habr.com/ru/post/1626437/


All Articles