# How do you join payloads with same topic and keyed objects?

**URL:** <https://discourse.nodered.org/t/how-do-you-join-payloads-with-same-topic-and-keyed-objects/75019>\
**Category:** General\
**Tags:** function-node\
**Created:** [10 February 2023 22:40 UTC](https://discourse.nodered.org/t/how-do-you-join-payloads-with-same-topic-and-keyed-objects/75019 "2023-02-10T22:40:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnnyPicnic](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnnypicnic/32/34787_2.png) [@JohnnyPicnic](https://discourse.nodered.org/u/JohnnyPicnic)\
**Post date:** [10 February 2023 22:40 UTC](https://discourse.nodered.org/t/how-do-you-join-payloads-with-same-topic-and-keyed-objects/75019/1 "2023-02-10T22:40:50Z")

</div>

I can't get the all my data to join after setting the same topic for each payload.  
My payload has three types. Temperature, Humidity and Pressure.  
Ideally I would like a result of one payload with:

```auto
{
  "Fri Feb 10 2023 12:00:00 GMT-0700 (Mountain Standard Time)": {
    "ATMOSPHERIC_PRESSURE": 853,
    "TEMPERATURE": 65,
    "HUMIDITY": 74
  },
  "Fri Feb 10 2023 12:30:00 GMT-0700 (Mountain Standard Time)": {
    "ATMOSPHERIC_PRESSURE": 752,
    "TEMPERATURE": 198,
    "HUMIDITY": 72
  },
  "Fri Feb 10 2023 13:00:00 GMT-0700 (Mountain Standard Time)": {
    "ATMOSPHERIC_PRESSURE": 817,
    "TEMPERATURE": 64.1,
    "HUMIDITY": 17
  },
  "Fri Feb 10 2023 13:30:00 GMT-0700 (Mountain Standard Time)": {
    "ATMOSPHERIC_PRESSURE": 773,
    "TEMPERATURE": 227,
    "HUMIDITY": 27
  }
}

```

The result I'm getting only outputs one of the sensors.  
 ![2023-02-10_15-40-17](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/5/f5ce40a47bee90dfcb794f297c1d99b758eb1d73.png)

```auto
{"payload":{"Fri Feb 10 2023 12:00:00 GMT-0700 (Mountain Standard Time)":{"ATMOSPHERIC_PRESSURE":853},"Fri Feb 10 2023 12:30:00 GMT-0700 (Mountain Standard Time)":{"ATMOSPHERIC_PRESSURE":752},"Fri Feb 10 2023 13:00:00 GMT-0700 (Mountain Standard Time)":{"ATMOSPHERIC_PRESSURE":817},"Fri Feb 10 2023 13:30:00 GMT-0700 (Mountain Standard Time)":{"ATMOSPHERIC_PRESSURE":773}},"_msgid":"fa07d1744983b353","topic":"2023-02-10T20:30:00.000Z"}

```

My Flow:

```auto
[{"id":"675390fa474fb666","type":"function","z":"d03c03b6d2f91f47","name":"measurementPeriod","func":"msg.measurementPeriod = msg.payload.measurementPeriodBase * msg.payload.measurementPeriodFactor\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1040,"y":280,"wires":[["81378a84bc7ada6d","b4fbaa1219fbccd1"]]},{"id":"b834cdc9633bbe11","type":"debug","z":"d03c03b6d2f91f47","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1560,"y":580,"wires":[]},{"id":"81378a84bc7ada6d","type":"split","z":"d03c03b6d2f91f47","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":670,"y":380,"wires":[["181c60c6663f838a"]]},{"id":"181c60c6663f838a","type":"switch","z":"d03c03b6d2f91f47","name":"channels","property":"parts.key","propertyType":"msg","rules":[{"t":"eq","v":"channels","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":920,"y":380,"wires":[["99b009ee7b2a4861"],[]]},{"id":"90f0f2704d744d6d","type":"switch","z":"d03c03b6d2f91f47","name":"Measurment Type","property":"payload.type","propertyType":"msg","rules":[{"t":"eq","v":"MEASUREMENT_TYPE_OK_ALARM","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":710,"y":460,"wires":[["29106dad075e522f"],["55fcce9c68c3fcdc"]]},{"id":"29106dad075e522f","type":"function","z":"d03c03b6d2f91f47","name":" numberOfMeasurements","func":"msg.numberOfMeasurements = 1 + (Math.abs([msg.payload.sampleOffsets][-1]) - 1) / msg.measurementPeriod\nmsg.numberOfMeasurements1 = msg.payload.sampleOffsets.length\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":990,"y":460,"wires":[[]]},{"id":"55fcce9c68c3fcdc","type":"switch","z":"d03c03b6d2f91f47","name":"Measurment Type","property":"channel.type","propertyType":"msg","rules":[{"t":"eq","v":"MEASUREMENT_TYPE_TEMPERATURE","vt":"str"},{"t":"eq","v":"MEASUREMENT_TYPE_ATMOSPHERIC_PRESSURE","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":690,"y":560,"wires":[["04c5b21205f53640"],["c715af1f95976fb8"],["895b89714e6a2f24"]]},{"id":"99b009ee7b2a4861","type":"split","z":"d03c03b6d2f91f47","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":1130,"y":380,"wires":[["e5b123514aa7fceb"]]},{"id":"e5b123514aa7fceb","type":"function","z":"d03c03b6d2f91f47","name":"function 5","func":"var flowpayload = flow.get(\"payload\")\n\nmsg.channel = msg.payload\nmsg.payload = flowpayload\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1320,"y":380,"wires":[["90f0f2704d744d6d"]]},{"id":"b4fbaa1219fbccd1","type":"change","z":"d03c03b6d2f91f47","name":"","rules":[{"t":"set","p":"payload","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1280,"y":280,"wires":[[]]},{"id":"04c5b21205f53640","type":"function","z":"d03c03b6d2f91f47","name":"TEMPERATURE","func":"\nfor (var i = 0; i < msg.channel.sampleOffsets.length; i++) {\n\n node.send({\n payload: {\n \n value: (msg.channel.startPoint + msg.channel.sampleOffsets[i]) /10,\n type: msg.channel.type.replace(\"MEASUREMENT_TYPE_\", \"\"),\n time: new Date((msg.channel.timestamp * 1000) + (msg.measurementPeriod * 1000 * i ))\n\n }\n })\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":970,"y":540,"wires":[["85aeed48b93bb237"]]},{"id":"895b89714e6a2f24","type":"function","z":"d03c03b6d2f91f47","name":"Other","func":"\n// @ts-ignore\nfor (var i = 0; i < msg.channel.sampleOffsets.length; i++) {\n\n node.send({\n payload: {\n \n value: (msg.channel.startPoint + msg.channel.sampleOffsets[i]),\n type: msg.channel.type.replace(\"MEASUREMENT_TYPE_\", \"\"),\n time: new Date((msg.channel.timestamp * 1000) + (msg.measurementPeriod * 1000 * i))\n\n }\n })\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":950,"y":620,"wires":[["85aeed48b93bb237"]]},{"id":"c715af1f95976fb8","type":"function","z":"d03c03b6d2f91f47","name":"ATMOSPHERIC_PRESSURE","func":"\n// @ts-ignore\nfor (var i = 0; i < msg.channel.sampleOffsets.length; i++) {\n\n node.send({\n payload: {\n\n value: (msg.channel.startPoint + msg.channel.sampleOffsets[i]) /10,\n type: msg.channel.type.replace(\"MEASUREMENT_TYPE_\", \"\"),\n time: new Date((msg.channel.timestamp * 1000) + (msg.measurementPeriod * 1000 * i ))\n\n }\n })\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":970,"y":580,"wires":[["85aeed48b93bb237"]]},{"id":"85aeed48b93bb237","type":"function","z":"d03c03b6d2f91f47","name":"function 12","func":"msg.topic = msg.payload.time\n\nvar sensortype = msg.payload.type\nvar value = msg.payload.value\n\nmsg.payload = {\n [sensortype]: value\n}\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1250,"y":580,"wires":[["ea2496f022fd62ef"]]},{"id":"ea2496f022fd62ef","type":"join","z":"d03c03b6d2f91f47","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"1","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1410,"y":580,"wires":[["b834cdc9633bbe11"]]},{"id":"46816d4ab6ab19a0","type":"inject","z":"d03c03b6d2f91f47","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":660,"y":280,"wires":[["b7e8a65c272e6e37"]]},{"id":"b7e8a65c272e6e37","type":"function","z":"d03c03b6d2f91f47","name":"function 14","func":"msg.payload = { \"serialNum\": \"EjRWeA==\", \"batteryStatus\": true, \"measurementPeriodBase\": 180, \"channels\": [{ \"type\": \"MEASUREMENT_TYPE_TEMPERATURE\", \"timestamp\": 1676055600, \"startPoint\": 650, \"sampleOffsets\": [0, 1330, -9, 1620] }, { \"type\": \"MEASUREMENT_TYPE_HUMIDITY\", \"timestamp\": 1676055600, \"startPoint\": 74, \"sampleOffsets\": [0, -2, -57, -47] }, { \"type\": \"MEASUREMENT_TYPE_ATMOSPHERIC_PRESSURE\", \"timestamp\": 1676055600, \"startPoint\": 8530, \"sampleOffsets\": [0, -1010, -360, -800] }], \"nextTransmissionAt\": 1676063488, \"transferReason\": 1, \"measurementPeriodFactor\": 10 }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":830,"y":280,"wires":[["675390fa474fb666"]]}]

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [10 February 2023 23:18 UTC](https://discourse.nodered.org/t/how-do-you-join-payloads-with-same-topic-and-keyed-objects/75019/2 "2023-02-10T23:18:42Z")

</div>

Holy smoke that's complicated!

We can see your input data

```auto
{"serialNum":"EjRWeA==","batteryStatus":true,"measurementPeriodBase":180,"channels":[{"type":"MEASUREMENT_TYPE_TEMPERATURE","timestamp":1676055600,"startPoint":650,"sampleOffsets":[0,1330,-9,1620]},{"type":"MEASUREMENT_TYPE_HUMIDITY","timestamp":1676055600,"startPoint":74,"sampleOffsets":[0,-2,-57,-47]},{"type":"MEASUREMENT_TYPE_ATMOSPHERIC_PRESSURE","timestamp":1676055600,"startPoint":8530,"sampleOffsets":[0,-1010,-360,-800]}],"nextTransmissionAt":1676063488,"transferReason":1,"measurementPeriodFactor":10}

```

Precisely what output are you trying to get?

---

<div class="post-metadata">

**Author:** ![JohnnyPicnic](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnnypicnic/32/34787_2.png) [@JohnnyPicnic](https://discourse.nodered.org/u/JohnnyPicnic)\
**Post date:** [10 February 2023 23:20 UTC](https://discourse.nodered.org/t/how-do-you-join-payloads-with-same-topic-and-keyed-objects/75019/3 "2023-02-10T23:20:45Z")

</div>

Yup, I included the input so hopefully it would be easier to get some help.  
A payload with each sensor nested into the same timestamp.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [10 February 2023 23:21 UTC](https://discourse.nodered.org/t/how-do-you-join-payloads-with-same-topic-and-keyed-objects/75019/4 "2023-02-10T23:21:26Z")

</div>

Yes but _precisely_ what output are you trying to get?

---

<div class="post-metadata">

**Author:** ![JohnnyPicnic](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnnypicnic/32/34787_2.png) [@JohnnyPicnic](https://discourse.nodered.org/u/JohnnyPicnic)\
**Post date:** [10 February 2023 23:25 UTC](https://discourse.nodered.org/t/how-do-you-join-payloads-with-same-topic-and-keyed-objects/75019/5 "2023-02-10T23:25:00Z")

</div>

Like what I put in the first post. The result I'm getting only includes the ATMOSPHERIC\_PRESSURE. There are also values for TEMPERATURE and HUMIDITY with the same timestamp.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [11 February 2023 00:38 UTC](https://discourse.nodered.org/t/how-do-you-join-payloads-with-same-topic-and-keyed-objects/75019/6 "2023-02-11T00:38:59Z")

</div>

I'm sorry but if you are not prepared to write down an example of the data that you want, in the right format, I can't begin to devise an algorithm to get there in Node-red.

Maybe someone else can help.

---

<div class="post-metadata">

**Author:** ![JohnnyPicnic](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnnypicnic/32/34787_2.png) [@JohnnyPicnic](https://discourse.nodered.org/u/JohnnyPicnic)\
**Post date:** [11 February 2023 02:08 UTC](https://discourse.nodered.org/t/how-do-you-join-payloads-with-same-topic-and-keyed-objects/75019/7 "2023-02-11T02:08:07Z")

</div>

I've edited my first post with the correct format.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [11 February 2023 08:17 UTC](https://discourse.nodered.org/t/how-do-you-join-payloads-with-same-topic-and-keyed-objects/75019/8 "2023-02-11T08:17:56Z")

</div>

Here is an example, I do not have a great deal of time today, so I hope it helps you. Will be available this evening if you have any questions (UK time).

```auto
[{"id":"46816d4ab6ab19a0","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"serialNum\": \"EjRWeA==\", \"batteryStatus\": true, \"measurementPeriodBase\": 180, \"channels\": [{ \"type\": \"MEASUREMENT_TYPE_TEMPERATURE\", \"timestamp\": 1676055600, \"startPoint\": 650, \"sampleOffsets\": [0, 1330, -9, 1620] }, { \"type\": \"MEASUREMENT_TYPE_HUMIDITY\", \"timestamp\": 1676055600, \"startPoint\": 74, \"sampleOffsets\": [0, -2, -57, -47] }, { \"type\": \"MEASUREMENT_TYPE_ATMOSPHERIC_PRESSURE\", \"timestamp\": 1676055600, \"startPoint\": 8530, \"sampleOffsets\": [0, -1010, -360, -800] }], \"nextTransmissionAt\": 1676063488, \"transferReason\": 1, \"measurementPeriodFactor\": 10 }","payloadType":"json","x":90,"y":4640,"wires":[["cd1b0337d0bca43c"]]},{"id":"cd1b0337d0bca43c","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$append(\t [$flowContext(\"storedata\")], \t [{\t \"Time\": $moment($$.payload.channels[0].timestamp * 1000),\t \"ATMOSPHERIC_PRESSURE\": $$.payload.channels[2].startPoint / 10,\t \"TEMPERATURE\": $$.payload.channels[0].startPoint / 10,\t \"HUMIDITY\": $$.payload.channels[1].startPoint\t}]\t)[]","tot":"jsonata"},{"t":"set","p":"storedata","pt":"flow","to":"payload","tot":"msg"},{"t":"set","p":"store_size","pt":"msg","to":"$count($$.payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":4640,"wires":[["05710b72738d8d05"]]},{"id":"05710b72738d8d05","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 231","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":490,"y":4640,"wires":[]}]

```

each new received message is appended to the stored data.

---

<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:** [12 April 2023 08:18 UTC](https://discourse.nodered.org/t/how-do-you-join-payloads-with-same-topic-and-keyed-objects/75019/9 "2023-04-12T08:18:40Z")

</div>

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