# Merge two payload messages based on same value in both payloads

**URL:** <https://discourse.nodered.org/t/merge-two-payload-messages-based-on-same-value-in-both-payloads/93562>\
**Category:** General\
**Created:** [29 November 2024 07:59 UTC](https://discourse.nodered.org/t/merge-two-payload-messages-based-on-same-value-in-both-payloads/93562 "2024-11-29T07:59:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaaw\_w0lf](https://avatars.discourse-cdn.com/v4/letter/k/ed8c4c/32.png) [@kaaw\_w0lf](https://discourse.nodered.org/u/kaaw_w0lf)\
**Post date:** [29 November 2024 07:59 UTC](https://discourse.nodered.org/t/merge-two-payload-messages-based-on-same-value-in-both-payloads/93562/1 "2024-11-29T07:59:46Z")

</div>

Hi there,  
I am currently struggling with the merging of two payload messages.  
I perform two http requests and receive a payload message back from both. Now I want to merge these two payloads using an identical value in both payloads.  
Below are the two payloads marked with the identical value.

 ![payload_2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/0/a087942f2b7b3cd93d6cedb1920d2a0a1e6014c1.png)  
 ![payload_1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/8/b8e4c0dc9954a5b384fc6e9651a3110b3d2852e9.png)  
Maybe someone can give me some advice?  
Thanks!

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [29 November 2024 08:08 UTC](https://discourse.nodered.org/t/merge-two-payload-messages-based-on-same-value-in-both-payloads/93562/2 "2024-11-29T08:08:41Z")

</div>

You may need to elaborate on what you want as a result.

Thoughts:  
`context` may help.  
You receive the _first_ payload and store it in `context`. Then when the second payload arrives code checks the existing and new payloads and if they _meet_ your needs, the new message is created.

But what the new message's structure is wasn't defined so I can't help you there.

---

<div class="post-metadata">

**Author:** ![kaaw\_w0lf](https://avatars.discourse-cdn.com/v4/letter/k/ed8c4c/32.png) [@kaaw\_w0lf](https://discourse.nodered.org/u/kaaw_w0lf)\
**Post date:** [29 November 2024 08:23 UTC](https://discourse.nodered.org/t/merge-two-payload-messages-based-on-same-value-in-both-payloads/93562/3 "2024-11-29T08:23:08Z")

</div>

Thanks for your answer!  
The new message should contain all the data from both messages combined under "st\_name".  
Hope that helps!

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [29 November 2024 08:31 UTC](https://discourse.nodered.org/t/merge-two-payload-messages-based-on-same-value-in-both-payloads/93562/4 "2024-11-29T08:31:56Z")

</div>

Have a look at this thread about merging messages...

> [@Merging Values to a single payload](https://discourse.nodered.org/t/merging-values-to-a-single-payload/92947/2):
>
> It is unclear to me what your problem is since you don't explain what the sources of the data are (what nodes they are), how you inject 'inject' the data. Please create a small flow demonstrating your problem and post it to a reply. In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```) ``` code goes here ``` You can edit and correct your post by clicking the pencil pencil2 icon. See this post…

Might be worth taking a look at this CookBook article as well...  
[https://cookbook.nodered.org/basic/join-streams](https://cookbook.nodered.org/basic/join-streams)

**EDIT:**  
The above method relies on the input messages to be merged having DIFFERENT topic-names.  
It is unclear what the topic-name is for your two sets of incoming data and whether a unique topic-name could be applied (at the source where the data is created).

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [29 November 2024 08:34 UTC](https://discourse.nodered.org/t/merge-two-payload-messages-based-on-same-value-in-both-payloads/93562/5 "2024-11-29T08:34:53Z")

</div>

Well, kinda.

But not really.

Ok, I'll step back and repose the question:

You are receiving messages and you want to merge them if..... _conditions are right_.

So does that mean 2 consecutive messages need to have their things meeting the requirements?

Basically - in my example - you are _caching_ a message.

You receive a message, and store it in `context`.  
If the next message meets things, then something needs to happen to merge the two.

If not: dump the first cache, and cache the newer message.

Repeat.

You will need to get that _structure_ understood as a bse line.

Something like:

```auto
let counter = context.get("counter") || 0

// if counter == 0 this is the first message
if (counter == 0)
{
    context.set("m1",msg.payload); // store this value
    context.set("counter",1)
    return; // exit. No further action needed
}

// now do stuff here to check the existing m1 and the new payload received.

// If they do NOT meet the requirement.
{
    context.set("m1",msg.payload); //store the value
    context.set("counter",0)
    return
}

// If they do match do stuff to combine the two messages.
// That is beyond me for what you want to use from both messages.

```

But that's a starting point.

---

<div class="post-metadata">

**Author:** ![kaaw\_w0lf](https://avatars.discourse-cdn.com/v4/letter/k/ed8c4c/32.png) [@kaaw\_w0lf](https://discourse.nodered.org/u/kaaw_w0lf)\
**Post date:** [29 November 2024 10:01 UTC](https://discourse.nodered.org/t/merge-two-payload-messages-based-on-same-value-in-both-payloads/93562/6 "2024-11-29T10:01:52Z")

</div>

Ok, first of all thanks again for your answers. I will just try to explain the current incoming data for better understanding what the result should look like:  
Currently I have two https requests from which I get multiple responses. The first request is generating the following messages (and much more):

 ![payload_1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/8/289502760aebc48825c0fc9d4983871ee60b415b.png)  
These are metadata information from some sensors we currently use.  
Simultaneously I get the following data from the second http request:  
 ![payload_2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/3/c3faf20516968ff24ba57182e86402e3048ee0a9.png)  
These are measurements from the sensors.  
Now I have for each sensor two datasets that I want to merge by the property "st\_name" because this value is included in both messages from the requests.  
Problem is that I have to push a requests to two different endpoints because there is no single endpoint which could give me all the data at once.  
Please let me know if it is better understandable now otherwise I will try to share more details.  
Thanks

---

<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:** [29 November 2024 10:30 UTC](https://discourse.nodered.org/t/merge-two-payload-messages-based-on-same-value-in-both-payloads/93562/7 "2024-11-29T10:30:13Z")

</div>

Give unique topics to the two sets of data and feed them into a Join node configured as in the example below, then you will get a single message containing both sets of data so you can build the merged data.

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

---

<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:** [27 February 2025 10:30 UTC](https://discourse.nodered.org/t/merge-two-payload-messages-based-on-same-value-in-both-payloads/93562/8 "2025-02-27T10:30:41Z")

</div>

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