AWS SQS JSON format when receiving message from SNS with Ruby SDK

I have an SQS queue which is subscribed to a SNS topic. When I publish a new notification to the topic, I use the following code (within a Sinatra app):

jsonMessage =  {
    "announcement" => {     
        "first_name" => results['first_name'][:s],  
                        "last_name" => results['last_name'][:s],
                        "loc_code" => results['location'][:s], 
                        "note" => params['note_content']
    }
}

msgid = @announcments_topic.publish(jsonMessage.to_json, 
                                    {subject: "Note Created",
                                     message_structure: 'json' })

When my queue listener picks up this notification, the message section of the corresponding hash looks like this:

"Message"=>"{"announcement":{"first_name":"Eve","last_name":"Salt","loc_code":"Location","note":"test"}}"

In my queue listener, I want to use this hash, but when I try to use

JSON.parse(result['Message'])

I get an unexpected token error because of the escaped double quotes. Any suggestions on how I can fix this? Am I not sending my notification as JSON properly? How can I get sns/sqs to not escape the double quotes?


Found the answer.

The problem was the way I was getting the JSON. I needed to use JSON.load(result['Message']) , instead of JSON.parse(...) .


SNS publish method actually apends escape character before publishing the message. Here is the doc http://docs.aws.amazon.com/sns/latest/api/API_Publish.html

JSON-specific constraints:

Keys in the JSON object that correspond to supported transport protocols must have simple JSON string values. The values will be parsed (unescaped) before they are used in outgoing messages. Outbound notifications are JSON encoded (meaning that the characters will be reescaped for sending). Values have a minimum length of 0 (the empty string, "", is allowed). Values have a maximum length bounded by the overall message size (so, including multiple protocols may limit message sizes). Non-string values will cause the key to be ignored. Keys that do not correspond to supported transport protocols are ignored. Duplicate keys are not allowed. Failure to parse or validate any key or value in the message will cause the Publish call to return an error (no partial delivery).

So in java, we get the json message using below instruction. this removes the escape character from the incoming message.

void handle(Message message) {

    **String serializedMessage = SNSMessage.fromJson(message).getMessage();**

}


You might also consider turning on raw message delivery on the topic subscription if you don't want to deal with the consumer having to remove the escape characters from the incoming messages.

Please refer to the following documentation in AWS:

http://docs.aws.amazon.com/sns/latest/dg/large-payload-raw-message.html

链接地址: http://www.djcxy.com/p/32198.html

上一篇: SQS不会收到通过SNS发布的消息

下一篇: 使用Ruby SDK从SNS接收消息时使用AWS SQS JSON格式