# Merge elements of objects to payload

**URL:** <https://discourse.nodered.org/t/merge-elements-of-objects-to-payload/63662>\
**Category:** General\
**Tags:** function-node\
**Created:** [9 June 2022 08:51 UTC](https://discourse.nodered.org/t/merge-elements-of-objects-to-payload/63662 "2022-06-09T08:51:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cysoz](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@cysoz](https://discourse.nodered.org/u/cysoz)\
**Post date:** [9 June 2022 08:51 UTC](https://discourse.nodered.org/t/merge-elements-of-objects-to-payload/63662/1 "2022-06-09T08:51:08Z")

</div>

Hello

I have signals, I would like merge elements Obiect and Obiect 2...  
my function not work,  
Important for me is mergle to payload

- (tagName 1, value 1, timestamp 1, value 2)
- (tagName 1, value 1, timestamp 1, value 3)

can you help me.

```auto
msg.payload : Object
object
deviceName: "cell_3"
**tagName: "HMI_H1_Temperature"**
deviceID: "49D9941E-8915-44E2-BCC9-0A0D0652389F"
success: true
datatype: "real"
timestamp: 1654751958598
**value: 37.457176208496094**
metadata: object
registerId: "E87D5CED-0734-4947-8963-0F111A6BCF1E"
description: "Temperatura H1 cela 3"

msg.payload : Object
object
deviceName: "cell_3"
tagName: "C31_Tracking_Data"
deviceID: "49D9941E-8915-44E2-BCC9-0A0D0652389F"
success: true
datatype: "word"
**timestamp: 1654752905277**
 **value: 0**
metadata: object
registerId: "C66C5852-2438-4BBA-B0BD-8C256F0E11A8"
description: ""

```

```auto
const a = new Date();
const top = msg.topic;
const pay = msg.payload;
const temp = msg.payload.success;
const temp1 = msg.payload.tagName;

if(temp && temp1)
{
 if(temp && temp1 === "C31_Tracking_Data")
    {
         let datafilter1 = msg.payload;
         let current1 = datafilter1.value;
         let obj1 = datafilter1.tagName;
         let previous1 = context.get(obj1);
        context.set(obj1,current1);  
     }

 if(temp && temp1 !== "C31_Tracking_Data") // Checks if the message succeeded and if the tag name exists before continuing.
    {
           let datafilter = msg.payload;
           let current = datafilter.value;
            let obj = datafilter.tagName;
            let previous = context.get(obj);
             context.set(obj,current);
    }  
          
            msg.topic = "Dom/SC3";
          msg.payload = {"location": obj, "utctimestamp_ms": a.getTime(), "code": current, "taskID": current1 };
   node.status({fill:"blue",shape:"dot",text: (obj, current)});
  return msg;
}

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [9 June 2022 08:54 UTC](https://discourse.nodered.org/t/merge-elements-of-objects-to-payload/63662/2 "2022-06-09T08:54:48Z")

</div>

> [@cysoz](#):
>
> Important for me is mergle to payload
> 
> - (tagName 1, value 1, timestamp 1, value 2)
> - (tagName 1, value 1, timestamp 1, value 3)

What do you expect the output to be from these 2 objects? Do you want all values avaiable in 1 message OR do you _really_ want values of 2 objects merged into 1 object & if so, then what happens when there are 2 properties with the same name but different values?

> [@cysoz](#):
>
> can you help me.

Yes, but you will need to correctly export the data - use the "copy value" button that appears under your mouse when you hover over the debug message properties.

---

<div class="post-metadata">

**Author:** ![cysoz](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@cysoz](https://discourse.nodered.org/u/cysoz)\
**Post date:** [9 June 2022 12:14 UTC](https://discourse.nodered.org/t/merge-elements-of-objects-to-payload/63662/3 "2022-06-09T12:14:34Z")

</div>

I have connect to PLC, , when parts stop on station I would like add to IDparts to fiew parametes.

- (tagName.paylad 2, value.paylad 2, timestamp.paylad 2, value.paylad 1)
- (tagName.paylad 3, value.paylad 3, timestamp.paylad 3, value.paylad 1)
- (tagName.paylad 4, value.paylad 4, timestamp.paylad 4, value.paylad 1)
- (tagName.paylad 5, value.paylad 5, timestamp.paylad 5, value.paylad 1)  
end next  
part go on next station,

---

<div class="post-metadata">

**Author:** ![cysoz](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@cysoz](https://discourse.nodered.org/u/cysoz)\
**Post date:** [9 June 2022 12:20 UTC](https://discourse.nodered.org/t/merge-elements-of-objects-to-payload/63662/4 "2022-06-09T12:20:28Z")

</div>

IDparts = payload 1 -\> C31\_Tracking\_Data  
**value: 10**  
data = payload 1,2,3,4.. -\> HMI\_H1\_Temperature  
**tagName: "HMI\_H1\_Temperature"**  
timestamp: 1654751958598  
**value: 37.457176208496094**

---

<div class="post-metadata">

**Author:** ![cysoz](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@cysoz](https://discourse.nodered.org/u/cysoz)\
**Post date:** [9 June 2022 12:22 UTC](https://discourse.nodered.org/t/merge-elements-of-objects-to-payload/63662/5 "2022-06-09T12:22:37Z")

</div>

> [@cysoz](#):
>
> ```auto
> 
> ```

```auto
[{"id":"c55613f7.93395","type":"Datahub Subscribe","z":"58fe519a.9abd2","name":"T_Lutowania C3H1","natc":"d16b4f91.27069","topicname":"devicehub.raw.49D9941E-8915-44E2-BCC9-0A0D0652389F.E87D5CED-0734-4947-8963-0F111A6BCF1E","x":130,"y":540,"wires":[["a4f63409.147c78"]]},{"id":"55148774.36ca88","type":"Datahub Subscribe","z":"58fe519a.9abd2","name":"C31_Tracking_Data","natc":"d16b4f91.27069","topicname":"devicehub.raw.49D9941E-8915-44E2-BCC9-0A0D0652389F.C66C5852-2438-4BBA-B0BD-8C256F0E11A8","x":130,"y":460,"wires":[["a4f63409.147c78","9e447256.be923"]]},{"id":"9e447256.be923","type":"debug","z":"58fe519a.9abd2","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":370,"y":400,"wires":[]},{"id":"aca0a33f.b5b3f","type":"debug","z":"58fe519a.9abd2","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":930,"y":500,"wires":[]},{"id":"a4f63409.147c78","type":"json","z":"58fe519a.9abd2","name":"JSON to Object","property":"payload","action":"obj","pretty":false,"x":360,"y":500,"wires":[["1c67c677.deddda"]]},{"id":"1c67c677.deddda","type":"function","z":"58fe519a.9abd2","name":"NewValue","func":"const a = new Date();\nconst top = msg.topic;\nconst pay = msg.payload;\nconst temp = msg.payload.success;\nconst temp1 = msg.payload.tagName;\nconst temp2 = msg.payload.value;\n\nif(temp1 === \"C31_Tracking_Data\" )\n {\n let trace = pay.value;\n \n }\n\n\nif(temp && temp1 !== \"C31_Tracking_Data\") // Checks if the message succeeded and if the tag name exists before continuing.\n{\n let datafilter = msg.payload;\n \n msg.payload.deviceID = \"trac\";\n let current = datafilter.value;\n \n \n let obj = datafilter.registerId;\n let previous = context.get(obj);\n \n if (previous !== current)\n {\n context.set(obj,current);\n node.status({fill:\"blue\",shape:\"dot\",text: (obj, current)});\n return msg;\n }\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":500,"wires":[["329ec114.ec3f7e","64dd267f.2203f8"]]},{"id":"329ec114.ec3f7e","type":"function","z":"58fe519a.9abd2","name":"NewName_SV","func":" const myObj = msg.payload;\n const text = myObj.tagName;\n \n\t\tif(text.startsWith(\"0#\") == true){\n\t\t\n\t\t\t//dla wartości 0#\n\t\t\t const restText = text.substr(\"0#\".length);\n\t\t myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/1;\n\t\t\tmsg.payload = myObj;\n\t\t return msg;\n\t\t\t}\n else if(text.startsWith(\"1#\") == true){\n\t\t\n\t\t\t//dla wartości 1# - dzielenie wartości przez 10\n\t\t \tconst restText = text.substr(\"1#\".length);\n\t\t myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/10;\n\t\t\tmsg.payload = myObj;\n\t\t return msg;\n\t\t\t}\n\t\telse if(text.startsWith(\"2#\") == true){\n\t\t\n\t\t\t//dla wartości 2# - dzielenie wartości przez 100\n\t\t \tconst restText = text.substr(\"2#\".length);\n\t\t myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/100;\n\t\t\tmsg.payload = myObj;\n\t\t return msg;\n\t\t\t}\n\t\telse if(text.startsWith(\"3#\") == true){\n\t\t\n\t\t\t//dla wartości 3# - dzielenie wartości przez 1000\n\t\t \tconst restText = text.substr(\"3#\".length);\n\t\t myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/1000;\n\t\t\tmsg.payload = myObj;\n\t\t return msg;\n\t\t\t}\n\t\telse if(text.startsWith(\"#\", 1 ) == false){\n\t\t\n\t\t\t//brak przedrostka\n\t\t\tmsg.payload = myObj;\n\t\t\treturn msg;\n\t\t }","outputs":1,"noerr":0,"initialize":"","finalize":"","x":700,"y":500,"wires":[["b86e757f.90d6c8","aca0a33f.b5b3f"]]},{"id":"b86e757f.90d6c8","type":"debug","z":"58fe519a.9abd2","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":440,"wires":[]},{"id":"64dd267f.2203f8","type":"debug","z":"58fe519a.9abd2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":380,"wires":[]},{"id":"d16b4f91.27069","type":"Datahub Connect","ipadd":"127.0.0.1","port":"4222"}]

```

---

<div class="post-metadata">

**Author:** ![cysoz](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@cysoz](https://discourse.nodered.org/u/cysoz)\
**Post date:** [9 June 2022 12:27 UTC](https://discourse.nodered.org/t/merge-elements-of-objects-to-payload/63662/6 "2022-06-09T12:27:57Z")

</div>

I can't download the data and add it.  
msg.payload = {"location": **tagName** , "utctimestamp\_ms": a.getTime() ,"code": **payload2.value** restCode, "taskID": " **payload1. value**"};

---

<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:** [23 June 2022 12:28 UTC](https://discourse.nodered.org/t/merge-elements-of-objects-to-payload/63662/7 "2022-06-23T12:28:50Z")

</div>

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