简体   繁体   English

在 Node-Red 中合并两个 JSON 对象

[英]Merging two JSON Objects in Node-Red

I am having some trouble trying to merge two JSON objects retrieved from my SQL Server database into a single object in Node-Red.我在尝试将从我的 SQL Server 数据库中检索到的两个 JSON 对象合并为 Node-Red 中的单个对象时遇到了一些麻烦。

The flow I have created is the following:我创建的流程如下: 在此处输入图片说明

For each call to the database I am receiving the following objects:对于对数据库的每次调用,我都会收到以下对象:

Plans:计划:

[{"PlanID":2,"Status":0,"EndTime":"0001-01-01T00:00:00.000Z"}]

Goals:目标:

[{"GoalID":1,"PlanID":2, "Type":2,"Message":"Walk 1000 km","Difficulty":0}]

I have created two functions which assign these objects into flow variables ('plans' and 'goals'), and now I was trying to merge both objects into a single JSON object.我创建了两个函数,将这些对象分配给流变量(“计划”和“目标”),现在我试图将这两个对象合并为一个 JSON 对象。

I don't know if I have to use the Join node for this purpose and if so how to configure it, but my idea was to create a JSON object in this format:我不知道我是否必须为此目的使用 Join 节点,如果是,如何配置它,但我的想法是以这种格式创建一个 JSON 对象:

[{"GoalID":1,"Plan":{"PlanID":2,"Status":0,"EndTime":"0001-01-01T00:00:00.000Z"}, "Type":2,"Message":"Walk 1000 km","Difficulty":0}]

First I wouldn't set them as flow variables as these will get over written if you get a second request in to the http-in node while the Database look ups are happening.首先,我不会将它们设置为流变量,因为如果在进行数据库查找时向 http-in 节点发送第二个请求,这些变量将被覆盖。 Better to add them as msg variables then they flow with the msg and can't be overwritten.最好将它们添加为msg变量,然后它们与msg一起流动并且不能被覆盖。

Given you are not just combining the 2 objects to get the super set of keys and values you are probably best off just using either a function node or the change to assemble to the output object yourself.鉴于您不仅仅是组合 2 个对象来获取键和值的超级集,您可能最好只使用函数节点或自己组装到输出对象的更改。

Assuming the input looks something like:假设输入看起来像:

msg.plans = [{"PlanID":2,"Status":0,"EndTime":"0001-01-01T00:00:00.000Z"}]
msg.goals = [{"GoalID":1,"PlanID":2, "Type":2,"Message":"Walk 1000 km","Difficulty":0}]

then the function node would look something like:那么函数节点看起来像:

msg.payload = msg.goals[0];
msg.payload.plan = msg.plans[0];
delete msg.goals;
delete msg.plans;
return msg;

The change node rules would looks something like更改节点规则看起来像

在此处输入图片说明

The join node would work to get the 2 objects into an array or an object using the topics as keys to hold the 2 input messages.连接节点将使用主题作为键来保存 2 个输入消息,从而将 2 个对象放入一个数组或一个对象中。

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

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