# How to combine two messages

**URL:** <https://discourse.nodered.org/t/how-to-combine-two-messages/36281>\
**Category:** General\
**Created:** [21 November 2020 10:20 UTC](https://discourse.nodered.org/t/how-to-combine-two-messages/36281 "2020-11-21T10:20:10Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![John245](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@John245](https://discourse.nodered.org/u/John245)\
**Post date:** [21 November 2020 10:20 UTC](https://discourse.nodered.org/t/how-to-combine-two-messages/36281/1 "2020-11-21T10:20:11Z")

</div>

I have a message with an array.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/6/c60b8c94f0010c3e8ad0eddce6e93c48f142ebca.png)

Using  
payload["000104391731"].state.remainingTime[0]  
and  
payload["000104391731"].state.remainingTime[1]

I can create two seperate messages.

## However I want to combine these values and create the following message 2h 45min

John

---

<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:** [21 November 2020 11:06 UTC](https://discourse.nodered.org/t/how-to-combine-two-messages/36281/2 "2020-11-21T11:06:01Z")

</div>

You can do, in a function node

```auto
let timeString = `${payload["000104391731"].state.remainingTime[0]}h ${payload["000104391731"].state.remainingTime[1]}min`

```

or you can do the same thing using jsonata in a change node.

---

<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:** [21 November 2020 11:28 UTC](https://discourse.nodered.org/t/how-to-combine-two-messages/36281/3 "2020-11-21T11:28:00Z")

</div>

There seems to be a bug in JSONata/Change node.  
I can set the inject to `payload["000104391731"].state.remainingTime[0] = 2`  
but JSONata or change node can not see it, but it visable in debug.

 ![Screenshot_20201121-112602](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/2/42993d70ef7e4bec2e183a0e992ba0ce7030ba52.png)  
example change JSONata flow

```auto
[{"id":"8b40e32d.d25c28","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload[\"000104391731\"].state.remainingTime[1]","v":"45","vt":"num"},{"p":"topic","vt":"str"},{"p":"payload[\"000104391731\"].state.remainingTime[0]","v":"2","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":170,"y":1880,"wires":[["15adae6d.934902"]]},{"id":"15adae6d.934902","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"hold0","pt":"msg","to":"payload[\"000104391731\"].state.remainingTime[0]","tot":"msg"},{"t":"set","p":"hold1","pt":"msg","to":"payload[\"000104391731\"].state.remainingTime[1]","tot":"msg"},{"t":"set","p":"timestrin","pt":"msg","to":"$string(hold0) & 'h ' & $string(hold1) & 'min'","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":1940,"wires":[["5e1bd60c.64727"]]},{"id":"5e1bd60c.64727","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":1960,"wires":[]}]

```

---

<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:** [21 November 2020 11:48 UTC](https://discourse.nodered.org/t/how-to-combine-two-messages/36281/4 "2020-11-21T11:48:31Z")

</div>

Set your debug node to Show Full Message and see what it shows

---

<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:** [21 November 2020 11:53 UTC](https://discourse.nodered.org/t/how-to-combine-two-messages/36281/5 "2020-11-21T11:53:44Z")

</div>

My mistake , while investigating.  
But the issue still remains in JSONata but not change node

```auto
[{"id":"d8f3269b.a43d9","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload[\"000104391731\"].state.remainingTime[1]","v":"45","vt":"num"},{"p":"topic","vt":"str"},{"p":"payload[\"000104391731\"].state.remainingTime[0]","v":"2","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":2120,"wires":[["6f97de47.86a5b"]]},{"id":"6f97de47.86a5b","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"timestrin","pt":"msg","to":"$string(payload[\"000104391731\"].state.remainingTime[0]) & 'h ' & $string(payload[\"000104391731\"].state.remainingTime[1]) & 'min'","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":230,"y":2180,"wires":[["e8309938.fb1628"]]},{"id":"e8309938.fb1628","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":500,"y":2200,"wires":[]}]

```

---

<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:** [21 November 2020 12:03 UTC](https://discourse.nodered.org/t/how-to-combine-two-messages/36281/6 "2020-11-21T12:03:53Z")

</div>

Did you realise the first one worked?

---

<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:** [21 November 2020 12:06 UTC](https://discourse.nodered.org/t/how-to-combine-two-messages/36281/7 "2020-11-21T12:06:55Z")

</div>

Yes, but you would have to move the payloads to get to the values.  
JSONata does not seem to like the path.

edit/ seems to be the 0 at start of object key.

---

<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:** [21 November 2020 12:13 UTC](https://discourse.nodered.org/t/how-to-combine-two-messages/36281/8 "2020-11-21T12:13:05Z")

</div>

Oh, I see the problem I think. JSONata syntax for properties with odd characters is different, can't remember what it is at the moment.

**[Edit]** Take out the square brackets and use dots instead (keep the quotes)

---

<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:** [21 November 2020 12:18 UTC](https://discourse.nodered.org/t/how-to-combine-two-messages/36281/9 "2020-11-21T12:18:35Z")

</div>

Yes the ["000104391731"] seems to be the issue,  
JSONata does not like the `[]` for objects, or object keys starting with numbers.  
Edit, That's it , was driving me potty, Cheers

Here is the JSONata example

```auto
[{"id":"e8309938.fb1628","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":490,"y":2120,"wires":[]},{"id":"d8f3269b.a43d9","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload[\"000104391731\"].state.remainingTime[1]","v":"45","vt":"num"},{"p":"topic","vt":"str"},{"p":"payload[\"000104391731\"].state.remainingTime[0]","v":"2","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":2120,"wires":[["6f97de47.86a5b"]]},{"id":"6f97de47.86a5b","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"timestrin","pt":"msg","to":" $string(payload.\"000104391731\".state.remainingTime[0]) \t & 'h ' & $string(\t payload.\"000104391731\".state.remainingTime[1]\t) & 'min'","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":2120,"wires":[["e8309938.fb1628"]]}]

```

---

<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:** [21 November 2020 12:57 UTC](https://discourse.nodered.org/t/how-to-combine-two-messages/36281/10 "2020-11-21T12:57:21Z")

</div>

> [@Colin](#):
>
> **Edit]** Take out the square brackets and use dots instead (keep the quote

I did search the online documentation, but could not see where this is mentioned, do you have a link?

---

<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:** [21 November 2020 13:29 UTC](https://discourse.nodered.org/t/how-to-combine-two-messages/36281/11 "2020-11-21T13:29:27Z")

</div>

I found it here [https://docs.jsonata.org/simple](https://docs.jsonata.org/simple)

---

<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:** [21 November 2020 13:35 UTC](https://discourse.nodered.org/t/how-to-combine-two-messages/36281/12 "2020-11-21T13:35:30Z")

</div>

I read that, but fail to see where it says use " " around path properties with odd characters.

---

<div class="post-metadata">

**Author:** ![John245](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@John245](https://discourse.nodered.org/u/John245)\
**Post date:** [21 November 2020 14:16 UTC](https://discourse.nodered.org/t/how-to-combine-two-messages/36281/13 "2020-11-21T14:16:12Z")

</div>

> [@E1cid](#):
>
> ```auto
> [{"id":"e8309938.fb1628","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":490,"y":2120,"wires":[]},{"id":"d8f3269b.a43d9","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload[\"000104391731\"].state.remainingTime[1]","v":"45","vt":"num"},{"p":"topic","vt":"str"},{"p":"payload[\"000104391731\"].state.remainingTime[0]","v":"2","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":2120,"wires":[["6f97de47.86a5b"]]},{"id":"6f97de47.86a5b","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"timestrin","pt":"msg","to":" $string(payload.\"000104391731\".state.remainingTime[0]) \t & 'h ' & $string(\t payload.\"000104391731\".state.remainingTime[1]\t) & 'min'","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":2120,"wires":[["e8309938.fb1628"]]}]
> 
> ```

Thanks a lot.

This did the trick. In first instance I was using the JSONNata but I did not manage to get it working.

* * *

John

---

<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:** [21 November 2020 15:47 UTC](https://discourse.nodered.org/t/how-to-combine-two-messages/36281/14 "2020-11-21T15:47:23Z")

</div>

> [@E1cid](#):
>
> I read that, but fail to see where it says use " " around path properties

The section I saw was  
Field references containing whitespace or reserved tokens can be enclosed in backticks

```auto
JSONata Other.`Over 18 ?`

```

But I now realise that says backticks, so it will need an expert to pronounce on how this is supposed to work in node-red.

---

<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:** [21 November 2020 15:52 UTC](https://discourse.nodered.org/t/how-to-combine-two-messages/36281/15 "2020-11-21T15:52:59Z")

</div>

Ok i see it now , cheers. It seems that " ` or ' will all work.

---

<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:** [5 December 2020 15:52 UTC](https://discourse.nodered.org/t/how-to-combine-two-messages/36281/16 "2020-12-05T15:52:59Z")

</div>

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