简体   繁体   English

使用Ruby SDK从SNS接收消息时,AWS SQS JSON格式

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

I have an SQS queue which is subscribed to a SNS topic. 我有一个已订阅SNS主题的SQS队列。 When I publish a new notification to the topic, I use the following code (within a Sinatra app): 当我向该主题发布新的通知时,我使用以下代码(在Sinatra应用程序内):

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部分如下所示:

"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? 我是否无法以JSON格式正确发送通知? How can I get sns/sqs to not escape the double quotes? 我怎样才能使sns / sqs不转义双引号?

Found the answer. 找到了答案。

The problem was the way I was getting the JSON. 问题是我获取JSON的方式。 I needed to use JSON.load(result['Message']) , instead of JSON.parse(...) . 我需要使用JSON.load(result['Message'])而不是JSON.parse(...)

SNS publish method actually apends escape character before publishing the message. SNS的发布方法实际上是在发布消息之前添加转义字符。 Here is the doc http://docs.aws.amazon.com/sns/latest/api/API_Publish.html 这是文档http://docs.aws.amazon.com/sns/latest/api/API_Publish.html

JSON-specific constraints: JSON特定的约束:

Keys in the JSON object that correspond to supported transport protocols must have simple JSON string values. JSON对象中与支持的传输协议相对应的键必须具有简单的JSON字符串值。 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). 出站通知是JSON编码的(这意味着将重新发送字符以进行发送)。 Values have a minimum length of 0 (the empty string, "", is allowed). 值的最小长度为0(允许使用空字符串“”)。 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). 无法解析或验证消息中的任何键或值将导致Publish调用返回错误(不分批发送)。

So in java, we get the json message using below instruction. 因此,在Java中,我们使用以下指令获取json消息。 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: 请参考AWS中的以下文档:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM