# Msg.payload splitting

**URL:** <https://discourse.nodered.org/t/msg-payload-splitting/47504>\
**Category:** General\
**Created:** [23 June 2021 00:19 UTC](https://discourse.nodered.org/t/msg-payload-splitting/47504 "2021-06-23T00:19:22Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjmotew](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sjmotew/32/29709_2.png) [@sjmotew](https://discourse.nodered.org/u/sjmotew)\
**Post date:** [23 June 2021 00:19 UTC](https://discourse.nodered.org/t/msg-payload-splitting/47504/1 "2021-06-23T00:19:22Z")

</div>

Hi!

Many posts on this, still having problems with what should be "simple"

Debug Output from node which collects an MQTT topic:

```auto
{"topic":"BlueIris/LND/Status","payload":"{ \"type\": \"MOTION_A\", \"trigger\": \"ON\" }","qos":0,"retain":false,"_msgid":"36e3e0a9.8d986"}

```

The two variable I want to change with function into msg are:

msg.topic=LND  
msg.payload=ON

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:** [23 June 2021 00:26 UTC](https://discourse.nodered.org/t/msg-payload-splitting/47504/2 "2021-06-23T00:26:16Z")

</div>

Ok, this isn't quite that simple.

The message (as you said) is coming from a `MQTT` node.

So the `payload` isn't quite that simple.

The _payload_ you really want to change is actually `msg.payload.trigger`.

You will need to use a `JSON` node just after the `MQTT IN` node to make it ..... correct.

Then you use the `change` node to do that.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![sjmotew](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sjmotew/32/29709_2.png) [@sjmotew](https://discourse.nodered.org/u/sjmotew)\
**Post date:** [23 June 2021 00:33 UTC](https://discourse.nodered.org/t/msg-payload-splitting/47504/3 "2021-06-23T00:33:29Z")

</div>

Thanks. That works for the payload, also I would like to extract only "LND" (actually the letters between the first and second fwd slash in that position) from the topic "BlueIris/ **LND** /Status"

---

<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:** [23 June 2021 00:35 UTC](https://discourse.nodered.org/t/msg-payload-splitting/47504/4 "2021-06-23T00:35:07Z")

</div>

Yeah, ok.

That would be done with a `SWITCH` node and set it for the `topic` rather than the `payload`.

Then (this isn't exactly what you want, but the best I can do) in the field set it to `contains` and put `LND`

Is that what you are wanting?

Something like this:

```auto
[{"id":"dcd802b1.9eae08","type":"json","z":"8bb4de19.f72c88","name":"","property":"payload","action":"","pretty":false,"x":420,"y":4250,"wires":[["daf3986b.deefc8","1462e6b4.eb9ff1"]]},{"id":"1462e6b4.eb9ff1","type":"switch","z":"8bb4de19.f72c88","name":"","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"LND","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":560,"y":4250,"wires":[["412ba252.458c9c"]]},{"id":"412ba252.458c9c","type":"debug","z":"8bb4de19.f72c88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":700,"y":4250,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![sjmotew](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sjmotew/32/29709_2.png) [@sjmotew](https://discourse.nodered.org/u/sjmotew)\
**Post date:** [23 June 2021 00:39 UTC](https://discourse.nodered.org/t/msg-payload-splitting/47504/5 "2021-06-23T00:39:16Z")

</div>

Thanks again. Not quite, the topic will change, may be LND or DVW etc., is there a way to extract the specific letters component from the topic using the "/\*\*\*/" meaning whatever the letters between the / and / are?

---

<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:** [23 June 2021 00:41 UTC](https://discourse.nodered.org/t/msg-payload-splitting/47504/6 "2021-06-23T00:41:50Z")

</div>

Hang on.

You said you wanted LND.... But now you want LND or DVW.

That is easy enough as you just add a second line in the `switch` node and make it `DVW`.

Yes, you could even go as far as making the texts `/LND/` and `/DVW/` to get better matching.

You then just expand on the `switch` node to _filter_ (not really) the messages you want to get through.

You then just send both outputs of the `switch` node to the same place - if the payloads are going to be handled the same way.

Like this:

```auto
[{"id":"1462e6b4.eb9ff1","type":"switch","z":"8bb4de19.f72c88","name":"","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"/LND/","vt":"str"},{"t":"cont","v":"/DVW/","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":560,"y":4250,"wires":[["412ba252.458c9c"],["412ba252.458c9c"]]}]

```

---

<div class="post-metadata">

**Author:** ![sjmotew](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sjmotew/32/29709_2.png) [@sjmotew](https://discourse.nodered.org/u/sjmotew)\
**Post date:** [23 June 2021 00:58 UTC](https://discourse.nodered.org/t/msg-payload-splitting/47504/7 "2021-06-23T00:58:24Z")

</div>

I think I figured it out, what I really need is a change node with JSONata:

```auto
[{"id":"49926122.1a257","type":"change","z":"ed4fc3d3.44bd8","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"$substring(msg.topic, 9, 3)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":820,"wires":[["e40e9885.143af8"]]}]

```

---

<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:** [23 June 2021 00:59 UTC](https://discourse.nodered.org/t/msg-payload-splitting/47504/8 "2021-06-23T00:59:25Z")

</div>

You've lost me with that, but if it works: no worries.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [23 June 2021 01:47 UTC](https://discourse.nodered.org/t/msg-payload-splitting/47504/9 "2021-06-23T01:47:40Z")

</div>

jsonata `$substring(msg.topic, 9, 3)` works .. but will the word after the first slash always start at position 9 and will be 3 characters ?

you could use javascript in a Function node to [split()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) the string in an array at the `/` character and pick the second element (independent of word length and char position)

```auto
msg.topic = msg.topic.split("/")[1]
return msg;

```

---

<div class="post-metadata">

**Author:** ![sjmotew](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sjmotew/32/29709_2.png) [@sjmotew](https://discourse.nodered.org/u/sjmotew)\
**Post date:** [23 June 2021 03:12 UTC](https://discourse.nodered.org/t/msg-payload-splitting/47504/10 "2021-06-23T03:12:17Z")

</div>

Thanks to everyone for the help, definitely pointed me in the right direction.

What I wanted to accomplish was to identify the MQTT topic that sat between the /../ knowing that it was variable depending on the alert that was set. (sometimes its LND, DVW, BACK... and pass this to a discrete object.

The solution was to use JSONata and regex to match the characters in between the / symbols. This was accomplished using a change node with the following expression:

```auto
$match(msg.topic,/(?<=\/).*(?=\/)/)

```

Was a bit tricky as the forward slash needs to be escaped first with a backslash and this employed both a lookback and lookforward.

```auto
[{"id":"49926122.1a257","type":"change","z":"ed4fc3d3.44bd8","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"$match(msg.topic,/(?<=\\/).*(?=\\/)/)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":820,"wires":[["e40e9885.143af8","2f685416.745b9c"]]}]

```

Thanks again for the direction and trials!

---

<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:** [7 July 2021 03:12 UTC](https://discourse.nodered.org/t/msg-payload-splitting/47504/11 "2021-07-07T03:12:20Z")

</div>

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