# Merger Node (Order of Objects)

**URL:** <https://discourse.nodered.org/t/merger-node-order-of-objects/77078>\
**Category:** General\
**Created:** [30 March 2023 12:02 UTC](https://discourse.nodered.org/t/merger-node-order-of-objects/77078 "2023-03-30T12:02:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![damica51](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@damica51](https://discourse.nodered.org/u/damica51)\
**Post date:** [30 March 2023 12:02 UTC](https://discourse.nodered.org/t/merger-node-order-of-objects/77078/1 "2023-03-30T12:02:46Z")

</div>

I'm using a merger node to merge several msg.payloads, BUT here is my problem. It will merge all of the msg.payloads but I really need them to be in the order they are read. How should I handle this?

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [30 March 2023 12:10 UTC](https://discourse.nodered.org/t/merger-node-order-of-objects/77078/2 "2023-03-30T12:10:58Z")

</div>

Searching for "merge" in `Manage Palette` spits out 13 hits.

Thus: Which node do you use? Please provide an example of the data coming in, the data currently as merged ... and the result you expect.

Then I'm convinced, people can support here...

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 March 2023 12:30 UTC](https://discourse.nodered.org/t/merger-node-order-of-objects/77078/3 "2023-03-30T12:30:39Z")

</div>

Also please show us some sample data so we can make sure we understand. If you are talking about the order of objects within an object then that should not make any difference. If you are talking about elements in an array then that is different.

---

<div class="post-metadata">

**Author:** ![damica51](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@damica51](https://discourse.nodered.org/u/damica51)\
**Post date:** [30 March 2023 12:47 UTC](https://discourse.nodered.org/t/merger-node-order-of-objects/77078/4 "2023-03-30T12:47:20Z")

</div>

![JoinSample](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/1/21bf2949917f1085cecc63d359fe2bd7071768f6.jpeg)  
Here is a picture, Each Http request creates an object (I have to do it this way because of the server I am reading from makes me do it this way)  
Then I "JOIN" them to create an Object(Array) This is where they may become sometimes out of order and I need them to stay in the order they are read.

I'm sorry I said in my first post "Merge" node as it is the "JOIN" node that come embedded with Node-Red.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [30 March 2023 13:02 UTC](https://discourse.nodered.org/t/merger-node-order-of-objects/77078/5 "2023-03-30T13:02:31Z")

</div>

🤯

This can be optimised somewhat!!

- I wouldn't use delay nodes
- No need for json nodes
- Put your HTTP requests in series.

Have your requests execute after each other - and set the request nodes to return a `Parsed JSON object` and finally, set the join node to return after the nth one.

Might be worth setting the request node to ignore the incoming payload also - so the last result, doesnt taint it and such.

funny thing, my image example can be further optimised - but you need to plan that carefully.  
mine not even be the best way - but should be much better than using delays and all that jazz

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/8/888b22047a64a9fc57db015dd584ef715a441b6e.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 March 2023 13:05 UTC](https://discourse.nodered.org/t/merger-node-order-of-objects/77078/6 "2023-03-30T13:05:02Z")

</div>

In fact you can use a message property to build the array into as you go. If you start at the beginning of that by setting `msg.result = []` then that property should be passed down the chain, and you can just push the next one each time. Then you don't need the Join node.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [30 March 2023 13:06 UTC](https://discourse.nodered.org/t/merger-node-order-of-objects/77078/7 "2023-03-30T13:06:23Z")

</div>

You have done this before 😉

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [30 March 2023 13:23 UTC](https://discourse.nodered.org/t/merger-node-order-of-objects/77078/8 "2023-03-30T13:23:08Z")

</div>

@damica51  
To build on the approach by Colin,

Your first function node will have

```js
msg.result = [];
msg.result.push(msg.payload)
return msg

```

Then subsequent function nodes will have

```js
msg.result.push(msg.payload)
return msg

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/1/412e04fc31234a195d2ed6a46aabecd93e2c3d9a.png)

I'm sure the `change` node with JSONata can sit here also.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [30 March 2023 16:39 UTC](https://discourse.nodered.org/t/merger-node-order-of-objects/77078/9 "2023-03-30T16:39:15Z")

</div>

I would push to something other than msg.payload since that might get overwritten by subsequent requests.

I would also put the request and process on a separate flow using a link-in and a link-out at the end set to return to a link-call node which is how you start the flow.

For the main process, start with an input of all of the URL's you need to request, then a split node to create multiple messages, each of which triggers the link-call.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/b/1b1c58f812e795ed6d163762732ea2fc3c57e2c0.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [29 May 2023 16:40 UTC](https://discourse.nodered.org/t/merger-node-order-of-objects/77078/10 "2023-05-29T16:40:13Z")

</div>

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