# Combining 2 parts of a msg.payload to form a Topic/Subtopic for publishing to MQTT

**URL:** https://discourse.nodered.org/t/combining-2-parts-of-a-msg-payload-to-form-a-topic-subtopic-for-publishing-to-mqtt/58177
**Category:** General
**Tags:** mqtt
**Created:** [11 February 2022 21:33 UTC](https://discourse.nodered.org/t/combining-2-parts-of-a-msg-payload-to-form-a-topic-subtopic-for-publishing-to-mqtt/58177 "2022-02-11T21:33:39Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![justjoe96](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/justjoe96/32/53017_2.png) [@justjoe96](https://discourse.nodered.org/u/justjoe96)
#### Post date: [11 February 2022 21:33 UTC](https://discourse.nodered.org/t/combining-2-parts-of-a-msg-payload-to-form-a-topic-subtopic-for-publishing-to-mqtt/58177/1 "2022-02-11T21:33:39Z")

</div>

Hello. I'm not sure if this is even possible.... I am trying to take 2 separate parts of a msg.payload and convert them into an MQTT formatted topic/subtopic.

For example, I want msg.topic to equal "msg.payload.parameter1 / msg.payload.parameter2" of the incoming node so when the message is then fed into an MQTT publish node, the topic seen at the broker will be parameter1/parameter2.

I can currently get the topic to set itself to parameter1 alone by using a change node but don't quite know the syntax for combining 2 parameters into a single topic separated by a slash for compatibility with MQTT.

---

<div class="post-metadata">

### Author: ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)
#### Post date: [11 February 2022 21:40 UTC](https://discourse.nodered.org/t/combining-2-parts-of-a-msg-payload-to-form-a-topic-subtopic-for-publishing-to-mqtt/58177/2 "2022-02-11T21:40:48Z")

</div>

I think the `join node` can solve this.

---

<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 2022 21:43 UTC](https://discourse.nodered.org/t/combining-2-parts-of-a-msg-payload-to-form-a-topic-subtopic-for-publishing-to-mqtt/58177/3 "2022-02-11T21:43:48Z")

</div>

Here is an example using JSONata in a change node, you could also use javascript in a function node.

```auto
[{"id":"7ec23cce.84de5c","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"param1\":\"param1\",\"param2\":\"param2\"}","payloadType":"json","x":140,"y":4400,"wires":[["beba9359.6c44f8"]]},{"id":"beba9359.6c44f8","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload.param1 & \"/\" & payload.param2","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":4420,"wires":[["e40ac006.709d5"]]},{"id":"e40ac006.709d5","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":4400,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![justjoe96](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/justjoe96/32/53017_2.png) [@justjoe96](https://discourse.nodered.org/u/justjoe96)
#### Post date: [11 February 2022 21:46 UTC](https://discourse.nodered.org/t/combining-2-parts-of-a-msg-payload-to-form-a-topic-subtopic-for-publishing-to-mqtt/58177/4 "2022-02-11T21:46:19Z")

</div>

This did it! Thank you!

---

<div class="post-metadata">

### Author: ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)
#### Post date: [11 February 2022 21:52 UTC](https://discourse.nodered.org/t/combining-2-parts-of-a-msg-payload-to-form-a-topic-subtopic-for-publishing-to-mqtt/58177/5 "2022-02-11T21:52:21Z")

</div>

Oops, I did not get the question, sorry 😶

---

<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 2022 22:54 UTC](https://discourse.nodered.org/t/combining-2-parts-of-a-msg-payload-to-form-a-topic-subtopic-for-publishing-to-mqtt/58177/6 "2022-02-11T22:54:01Z")

</div>

Mostly correct, you could split and join.

```auto
[{"id":"5c069ba0.5d188c","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"param1\":\"param1\",\"param2\":\"param2\"}","payloadType":"json","x":170,"y":4440,"wires":[["c53201a1.c945f"]]},{"id":"c53201a1.c945f","type":"split","z":"c791cbc0.84f648","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":330,"y":4360,"wires":[["97f47398.fe9e1"]]},{"id":"97f47398.fe9e1","type":"join","z":"c791cbc0.84f648","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"/","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":450,"y":4360,"wires":[["2dc9bff.e0ff24"]]},{"id":"2dc9bff.e0ff24","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":4360,"wires":[["ccc79603.d75458"]]},{"id":"ccc79603.d75458","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":4440,"wires":[]}]

```

For completeness you can also use a template

```auto
[{"id":"4a1983d9.a7083c","type":"template","z":"c791cbc0.84f648","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.param1}}/{{payload.param2}}","output":"str","x":380,"y":4280,"wires":[["e40ac006.709d5"]]},{"id":"7ec23cce.84de5c","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"param1\":\"param1\",\"param2\":\"param2\"}","payloadType":"json","x":190,"y":4280,"wires":[["4a1983d9.a7083c"]]},{"id":"e40ac006.709d5","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":4300,"wires":[]}]

```

---

<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: [25 February 2022 22:54 UTC](https://discourse.nodered.org/t/combining-2-parts-of-a-msg-payload-to-form-a-topic-subtopic-for-publishing-to-mqtt/58177/7 "2022-02-25T22:54:13Z")

</div>

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