15

I see in the examples how to to pass a message string to amazon sns sdk's publish method. However, is there an exmaple of how to pass a custom object as the message? I tried setting "MessageStructure" to "json" but then I get InvalidParameter: Invalid parameter: Message Structure - No default entry in JSON message body error. Where should I be passing the object values into in the params?

Any examples?

    var params = {
        Message: JSON.stringify(item),
        MessageStructure: 'json',
        TopicArn: topic
        //MessageAttributes: item
    };

    return sns.publishAsync(params);

4 Answers 4

21

There is no SDK-supported way to pass a custom object as a message-- messages are always strings. You can, of course, make the string a serialized version of your object.

MessageStructure: 'json' is for a different purpose-- when you want to pass different strings to different subscription types. In that case, you make the message a serialized json object with AWS-defined structure, where each element defines the message to send to a particular type of subscription (email, sqs, etc). Even in that case, the messages themselves are just strings.

MessageAttributes are parameters you add to the message to support specific subscription types. If you are using SNS to talk to Apple's IOS notification service, for example, you might have to supply additional message parameters or authentication keys-- MessageAttributes provide a mechanism to do this. This is described in this AWS documentation.

3
  • Ok, cool - what is messageattributes then? I can't figure out what that property is for from the documentation. Jul 14, 2015 at 21:55
  • @MonkeyBonkey -- Added info to answer
    – antlersoft
    Jul 14, 2015 at 22:09
  • 3
    Thanks for the clarification. The way works for me is removing the MessageStructure: 'JSON'.
    – Allen
    Dec 12, 2019 at 12:05
6

An example is shown here: https://docs.aws.amazon.com/sns/latest/api/API_Publish.html#API_Publish_Example_2

The JSON format for Message is as follows:

{
    "default": "A message.",
    "email": "A message for email.",
    "email-json": "A message for email (JSON).",
    "http": "A message for HTTP.",
    "https": "A message for HTTPS.",
    "sqs": "A message for Amazon SQS." 
} 

So, assuming what you wanted to pass is an object, the way it worked for me was:

const messageObjToSend = {
    ...
}

const params = {
    Message: JSON.stringify({
        default: JSON.stringify( messageObjToSend )
    }),
    MessageStructure: 'json',
    TopicArn: 'arn:aws:sns...'
}
2
  • 1
    That is the wrong answer. MessageStructure is not used as you are providing. See antlersoft's answer above: stackoverflow.com/a/31418009/260261
    – mbadawi23
    Jan 3, 2022 at 23:14
  • It works in the sense that the message is sent, but yes, you're correct - MessageStructure is more for when you've different subscription types. @everyone - ignore this message and just serialise your object per stackoverflow.com/a/31418009/639441 :D Jan 5, 2022 at 15:22
2

Jackson 2 has pretty good support to convert object to JSON String and vice versa.

To String

Cat c = new Cat();
ObjectMapper mapper = new ObjectMapper();
String s = mapper.writeValueAsString(c);

To Object

Cat obj = mapper.readValue(s,Cat.class);
2

The message needs to be a JSON object and the default property needs to be added and should contain the JSON you want included in the email.

    var defaultMessage = { "default": item };

    var params = {
        Message: defaultMessage, /*JSON.stringify(item),*/
       ---------^
        MessageStructure: 'json',
        TopicArn: topic
        //MessageAttributes: item
    };

    return sns.publishAsync(params);

Using python,

boto3.client("sns").publish(
        TopicArn=sns_subscription_arn,
        Subject="subject",
        Message=json.dumps({"default": item}),
                    --------^
        MessageStructure="json",
    )

FYI, if you go to this SNS topic in the AWS Console you can "publish message" and choose "Custom payload for each delivery protocol.". Here you will see a template of the email and the "default" property is tagged for "Sample fallback message".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.