如何将一个消息中包含的JSON对象数组转换为多个JSON对象消息?

时间:2016-03-03 14:55:24

标签: javascript json node.js ibm-cloud node-red

当前创建一个在Node-RED上运行的ETL流,在Node.js上运行,在CloudFoundry上运行的Bluemix上运行。

我正在查询Cloudant数据库,并将包含所有文档的单个JSON数组返回的查询结果作为JSON对象。

不,我想以流水线的方式单独处理它们。如何在Node-RED中实现这一目标?

非常感谢!

JSON

[{
  "id": "c8342b2a.bf0df",
  "type": "cloudant in",
  "z": "2f0a54f6.110ccc",
  "service": "nokeredrkie-cloudantNoSQLDB",
  "cloudant": "",
  "name": "",
  "database": "tweets",
  "search": "_all_",
  "design": "",
  "index": "",
  "x": 295.70001220703125,
  "y": 450.9333190917969,
  "wires": [
    ["5e83a34.48cdd5c"]
  ]
}, {
  "id": "c9411e75.2b1b7",
  "type": "debug",
  "z": "2f0a54f6.110ccc",
  "name": "",
  "active": true,
  "console": "false",
  "complete": "payload",
  "x": 646.699951171875,
  "y": 470.7333679199219,
  "wires": []
}, {
  "id": "3f1d2018.8f5ec8",
  "type": "inject",
  "z": "2f0a54f6.110ccc",
  "name": "",
  "topic": "",
  "payload": "",
  "payloadType": "date",
  "repeat": "",
  "crontab": "",
  "once": true,
  "x": 126.70001220703125,
  "y": 472.6666564941406,
  "wires": [
    ["c8342b2a.bf0df"]
  ]
}, {
  "id": "5e83a34.48cdd5c",
  "type": "function",
  "z": "2f0a54f6.110ccc",
  "name": "",
  "func": "//msg.payload = JSON.parse(msg.payload);\nmsg.payload = msg.payload[1].tweet.user.screen_name;\nreturn msg;",
  "outputs": 1,
  "noerr": 0,
  "x": 444.70001220703125,
  "y": 489.4000549316406,
  "wires": [
    ["c9411e75.2b1b7"]
  ]
}]

1 个答案:

答案 0 :(得分:1)

Cloudant节点输出到功能节点,如下所示:

var msgs = [];
for (var i = 0; i< msg.payload.length; i++) {
  msgs.push({payload: msg.payload[i]});
}

return [msgs]

这会将数组中的每个项目流式传输到流程中的下一个节点

编辑:需要将数组包装在另一组[]中,以便将所有消息输出到相同的输出中。