【问题标题】:Sending a json using SQS and posting it使用 SQS 发送 json 并发布
【发布时间】:2019-01-27 06:03:12
【问题描述】:

我将一个 json 对象/字符串(两者都尝试过)放入 SQS 队列 - 我试图将其作为有效负载发布到 lambda 函数中,当 SQS 中有一个新项目时触发该函数。

我的问题是 json 在另一端出现,在我拥有的每个 "、json 对象或字符串中添加了额外的 \(反斜杠)。

这是我添加到 SQS 的部分 json 作为纯字符串(这是要求)如何在另一端出现的示例:

"{\"properties\":{\"colorIconValue\":\"Other\",\"description\":\"According to media reports on Saturday, 26 January, 

虽然文本进入队列时看起来像这样:

{"properties": {"colorIconValue": "Other","description": "International media r

将 json 添加到队列的代码来自 AWS 蓝图:

    console.log("plain text" + incident_report);
                         var params = {
                            MessageBody: incident_report,
                            QueueUrl: QUEUE_URL
                        };
                        //write to SQS
                        sqs.sendMessage(params, function(err,data){
                          if(err) {
                            console.log('error:',"Fail Send Message" + err);
                            context.done('error', "ERROR Put SQS");  // ERROR with message
                          }else{
                            //console.log('data:',data.MessageId);
                            context.done(null,'');  // SUCCESS 
                          }
                        });

我尝试将有效负载添加为 JSON 对象和 JSON.stringfy(),但我得到了相同的结果...稍后无法发布(http 帖子)。

【问题讨论】:

    标签: javascript node.js json string amazon-sqs


    【解决方案1】:

    在收到的 SQS 消息中使用 JSON.parse()。例如:

    sqs.receiveMessage(params, function(err, data) {
      if (err) throw err;
    
      // Parse your data to received string to json
      const jsonObject = JSON.parse(data.Records[0].body);
    });
    

    这应该在jsonObject 中保存一个您想要的对象。

    【讨论】:

      猜你喜欢
      • 2018-10-19
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 2018-06-23
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多