Need help combining two msg with arrays

I have two messages that each have an array that is called the same (line_items).

122233

I need to combine them so that I get one message, with an array called line_items, that has both of the objects inside it.

I have tried using the join node in manual mode with different type of settings, but the closest I can get is this, where the arrays are placed inside a new array:
122233

But I need the objects to be in the array directly (in the "root", if that makes sense?)
Like this (photoshopped):

mockup

How do you differentiate them?

Do they come from the same node wire or separate wires?

show a picture of the flow where the messages come from

Without this info, we would be guessing.

Hello,

Assuming you have only two items, you can come up with something like this using a function node, It might not be the best way to do it, but you can give it a try I guess ! :slight_smile:

(For my method to work you'll need to have differents topics name on your messages to be able to differentiate them, and also I deleted my previous post cause I made a mistake)

[{"id":"a877e7f86dc43baa","type":"inject","z":"4649623030b5e549","name":"","props":[{"p":"payload.line_items","v":"[{\"item1\":\"item1\"}]","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"item1","x":310,"y":1000,"wires":[["96c64f3e38f8254a"]]},{"id":"f0ad6519098139b0","type":"inject","z":"4649623030b5e549","name":"","props":[{"p":"payload.line_items","v":"[{\"item2\":\"item2\"}]","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"item2","x":310,"y":1060,"wires":[["96c64f3e38f8254a"]]},{"id":"5416325db8307a6f","type":"debug","z":"4649623030b5e549","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":1040,"wires":[]},{"id":"96c64f3e38f8254a","type":"function","z":"4649623030b5e549","name":"","func":"var local_msg = RED.util.cloneMessage(msg);\nflow.set(local_msg.topic, local_msg);\n\nvar item1 = flow.get(\"item1\") || \"\";\nvar item2 = flow.get(\"item2\") || \"\";\n\nvar new_item = {};\nnew_item.payload = {};\nnew_item.payload.line_items = [];\n\nif(item1.topic === \"item1\" && item2.topic === \"item2\"){\n    new_item.payload.line_items.push(item1.payload.line_items[0])\n    new_item.payload.line_items.push(item2.payload.line_items[0])\n\n    return new_item;\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":1040,"wires":[["5416325db8307a6f"]]}]

Thank you! This works perfectly if both item1 and item2 exist.

However, in some cases, only a message with topic of item1 exists. In such cases, I want the message (item1) to be passed through (right now, the function node waits for item2 to arrive, which it never will).

Is that possible to do? :smiley:

Like this:

What will determine if msg2 is not gong to show up?

I do a check earlier in the flow where a change node looks for a certain item in the message, if the value of that item is greater than 0, the message passes on.

Then I would put a switch node in to test that condition and bypass the join node if it was true otherwise go to the join node.

Thank you for the solution, it works.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.