# Help getting a substring from payload

**URL:** <https://discourse.nodered.org/t/help-getting-a-substring-from-payload/92536>\
**Category:** General\
**Created:** [21 October 2024 06:57 UTC](https://discourse.nodered.org/t/help-getting-a-substring-from-payload/92536 "2024-10-21T06:57:28Z")\
**Posts on this page:** 8\
**Page:** 1

<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:** [21 October 2024 06:57 UTC](https://discourse.nodered.org/t/help-getting-a-substring-from-payload/92536/1 "2024-10-21T06:57:28Z")

</div>

Sorry folks, seems I can't work out how to do this.

eg:  
`2024-10-21 17:45:18 UpLink Online`

I want only the last part: `UpLink Online`

(Given `UpLink` and `Online` can change)

I have the `string` node, but I seem to be missing something how to get that done.

Or would it be better to use a `function` node and do some JS magic?

🤷

EDIT:  
This seems to work.

```auto
[{"id":"d04cbd8011adb764","type":"string","z":"0918ee609bf69fc7","name":"","methods":[{"name":"delLeftMost","params":[{"type":"str","value":":"}]},{"name":"delLeftMost","params":[{"type":"str","value":":"}]},{"name":"delLeftMost","params":[{"type":"str","value":" "}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":2890,"y":5530,"wires":[["35e4f569a22b2606"]]}]

```

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [21 October 2024 07:21 UTC](https://discourse.nodered.org/t/help-getting-a-substring-from-payload/92536/2 "2024-10-21T07:21:25Z")

</div>

If the length of the payload is always constant (at least the part which you don't want) I think this should work using Jsonata $substring in a change node,

```auto
[{"id":"deccf7af512c5094","type":"inject","z":"5a5b50a5e5a3beba","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2024-10-21 17:45:18 UpLink Online","payloadType":"str","x":600,"y":3200,"wires":[["ea3ff037815b9f80"]]},{"id":"ea3ff037815b9f80","type":"change","z":"5a5b50a5e5a3beba","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$substring(payload, 20, 20)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":3200,"wires":[["d88f5bdba063c609"]]},{"id":"d88f5bdba063c609","type":"debug","z":"5a5b50a5e5a3beba","name":"debug 2731","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1045,"y":3200,"wires":[]}]

```

---

<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:** [21 October 2024 07:21 UTC](https://discourse.nodered.org/t/help-getting-a-substring-from-payload/92536/3 "2024-10-21T07:21:53Z")

</div>

How do you feel about regular expressions?

You could use a change node to remove the date and time portion.

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

Explanation: `[0-9\-:] *` matches any character which is a digit, hyphen or colon, followed by zero or more spaces. (It seems to work without the `\` to escape any special meaning of the second hyphen too)

Your method will be easier to understand a year from now!

Edit: I like @smanjunath211 's solution too, only except that his debug node is named `debug 2731` 😱

---

<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:** [21 October 2024 08:04 UTC](https://discourse.nodered.org/t/help-getting-a-substring-from-payload/92536/4 "2024-10-21T08:04:10Z")

</div>

Thanks both.

Yeah, _in a few years time_ - who knows..... 😉

So the `string` node hasn't got the stuff needed, huh?

I'll have to try to remember that too - for next time.

WRT the `debug` node... I'm not much better sometimes. 😉

I may try what I did but keep these as options if it fails for unknown things I haven't thought of.

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [21 October 2024 08:59 UTC](https://discourse.nodered.org/t/help-getting-a-substring-from-payload/92536/5 "2024-10-21T08:59:09Z")

</div>

> [@jbudd](#):
>
> named `debug 2731`

Not entirely my fault, inherited from forum during example downloads! but never bothered to clean up and renumber them 🙂

---

<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:** [21 October 2024 09:17 UTC](https://discourse.nodered.org/t/help-getting-a-substring-from-payload/92536/6 "2024-10-21T09:17:34Z")

</div>

> [@smanjunath211](#):
>
> Not entirely my fault, inherited from forum

We've all done it!  
If we could search the forum for the very first `debug 1000+` I hope it wouldn't turn out to be my fault!

---

<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 October 2024 09:44 UTC](https://discourse.nodered.org/t/help-getting-a-substring-from-payload/92536/7 "2024-10-21T09:44:04Z")

</div>

Here is a flow that will renumber all the numbered debug nodes in a flows file. Configure the paths in the inject node, run the flow, replace the flows file with the one generated by the flow and restart node red. You do, of course, already have a backup of your flows file just in case.

```auto
[{"id":"2a1220f4e0f4ad64","type":"group","z":"bdd7be38.d3b55","name":"Rationalise debug node numbering","style":{"label":true},"nodes":["4cbc1bf30977dc4d","b6c70c743e51889d","8b2ca0887d7c0a3e","5b51b44a83135ec3","fefb82892cbc35d0","e60ea84d9655fc06","f6a6e6641bad366a"],"x":174,"y":3339,"w":972,"h":202},{"id":"4cbc1bf30977dc4d","type":"inject","z":"bdd7be38.d3b55","g":"2a1220f4e0f4ad64","name":"Configure paths in here","props":[{"p":"flowfilePath","v":"/home/colinl/.node-red/flows.json","vt":"str"},{"p":"outPath","v":"/home/colinl/.node-red/flows.new.json","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":320,"y":3380,"wires":[["b6c70c743e51889d"]]},{"id":"b6c70c743e51889d","type":"file in","z":"bdd7be38.d3b55","g":"2a1220f4e0f4ad64","name":"Read flow file","filename":"flowfilePath","filenameType":"msg","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":550,"y":3380,"wires":[["5b51b44a83135ec3"]]},{"id":"8b2ca0887d7c0a3e","type":"debug","z":"bdd7be38.d3b55","g":"2a1220f4e0f4ad64","name":"debug 14","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":910,"y":3500,"wires":[]},{"id":"5b51b44a83135ec3","type":"json","z":"bdd7be38.d3b55","g":"2a1220f4e0f4ad64","name":"","property":"payload","action":"","pretty":false,"x":750,"y":3380,"wires":[["fefb82892cbc35d0"]]},{"id":"fefb82892cbc35d0","type":"function","z":"bdd7be38.d3b55","g":"2a1220f4e0f4ad64","name":"Rationalise debug node names","func":"// msg.payload should include a flow file as an array of objects\nlet debugNumber = 1\nmsg.payload = msg.payload.map(rationalise) \nreturn msg;\n\nfunction rationalise(item) {\n let answer = item\n if (item.type === \"debug\") {\n // check for name \"debug nnn\"\n if (item.name.match(/debug [0-9]+$/)) {\n //node.warn(`matching debug node found: ${JSON.stringify(item)}`)\n // Change the name to an incrementing debug number\n answer.name = `debug ${debugNumber++}`\n //node.warn(`changed to: ${JSON.stringify(answer)}`)\n }\n }\n return answer\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":990,"y":3380,"wires":[["f6a6e6641bad366a"]]},{"id":"e60ea84d9655fc06","type":"file","z":"bdd7be38.d3b55","g":"2a1220f4e0f4ad64","name":"Write flow file","filename":"outPath","filenameType":"msg","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"none","x":730,"y":3460,"wires":[["8b2ca0887d7c0a3e"]]},{"id":"f6a6e6641bad366a","type":"json","z":"bdd7be38.d3b55","g":"2a1220f4e0f4ad64","name":"","property":"payload","action":"","pretty":true,"x":550,"y":3460,"wires":[["e60ea84d9655fc06"]]}]

```

---

<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:** [4 November 2024 09:44 UTC](https://discourse.nodered.org/t/help-getting-a-substring-from-payload/92536/8 "2024-11-04T09:44:14Z")

</div>

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