# Message payload - String?

**URL:** <https://discourse.nodered.org/t/message-payload-string/26494>\
**Category:** General\
**Created:** [11 May 2020 16:42 UTC](https://discourse.nodered.org/t/message-payload-string/26494 "2020-05-11T16:42:55Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![uproden](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/uproden/32/21708_2.png) [@uproden](https://discourse.nodered.org/u/uproden)\
**Post date:** [11 May 2020 16:42 UTC](https://discourse.nodered.org/t/message-payload-string/26494/1 "2020-05-11T16:42:55Z")

</div>

I have been using NodeRed for quite a while and have never seen this before. I am using the "file" in node to grab the contents of a file and it seems to work fine, but it's outputting the message payload as a "string" in a way I have never seen before

![String](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/1/316e015e119d112f7a429e540ca42465893005c7.jpeg)

I have a switch node after the file in and when doing a payload == some\_value it does not work, if I use "contains" it works but that is not ideal in this situation, plus I would like to understand what this is. I have never seen a payload be marked as "string" this way. Any help would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [11 May 2020 16:55 UTC](https://discourse.nodered.org/t/message-payload-string/26494/2 "2020-05-11T16:55:43Z")

</div>

That means it contains a newline character. If you click on it again to collapse back down, it'll look something like this:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/9/f9a4647915b3a0067859745edb69d402e217c351.png)

You can see the newline meta character is displayed at the end.

This is why you can't do a direct comparison - you'll need to strip off the newline before you can do that.

---

<div class="post-metadata">

**Author:** ![uproden](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/uproden/32/21708_2.png) [@uproden](https://discourse.nodered.org/u/uproden)\
**Post date:** [11 May 2020 18:40 UTC](https://discourse.nodered.org/t/message-payload-string/26494/3 "2020-05-11T18:40:28Z")

</div>

Oh wow yeah that was the problem, **thank you so much!** What would be the easiest way to strip return off the end, I have never had to deal with this one before.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [11 May 2020 18:46 UTC](https://discourse.nodered.org/t/message-payload-string/26494/4 "2020-05-11T18:46:02Z")

</div>

Have a look at the trim() method:

> **[String.prototype.trim()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)**
>
> The trim() method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).

---

<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:** [11 May 2020 20:07 UTC](https://discourse.nodered.org/t/message-payload-string/26494/5 "2020-05-11T20:07:10Z")

</div>

Alternatively you could leave it there and use a regex compare to look for something like `^yo\s$` (untested) which matches start of line followed by yo then one white space character and then end of line (I think, I always have to play about a bit to get regexs to work as I don't use them very often).

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [11 May 2020 20:22 UTC](https://discourse.nodered.org/t/message-payload-string/26494/6 "2020-05-11T20:22:41Z")

</div>

I always found [regex101.com](http://regex101.com) to be a good place to fiddle with regex expressions. I think the carriage return which is the problem here is `\r` in a regex if your trying to build one.  
Happy forum anniversary by the way @Colin

---

<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:** [11 May 2020 20:45 UTC](https://discourse.nodered.org/t/message-payload-string/26494/7 "2020-05-11T20:45:49Z")

</div>

> [@JGKK](#):
>
> the problem here is `\r` in a regex

`\s` should match CR or LF (or other white space) so saves trying to work out which symbol is which.

> [@JGKK](#):
>
> Happy forum anniversary by the way

Thanks. 🙂

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [11 May 2020 20:57 UTC](https://discourse.nodered.org/t/message-payload-string/26494/8 "2020-05-11T20:57:49Z")

</div>

Sometimes it can be stubborn; to remove the return or next line I keep this in my notes file.

Function to remove next line:

```auto
msg.payload = msg.payload.replace(/\n/g,"");
return msg;

```

Function to remove Return:

```auto
msg.payload = msg.payload.replace(/\r/g,"");
return msg;

```

You can remove both too!:

```auto
msg.payload = msg.payload.replace(/[\r\n]/g, "");
return msg;

```

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [11 May 2020 21:00 UTC](https://discourse.nodered.org/t/message-payload-string/26494/9 "2020-05-11T21:00:24Z")

</div>

this is what trim() does, no need for a regex replace.

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [11 May 2020 21:12 UTC](https://discourse.nodered.org/t/message-payload-string/26494/10 "2020-05-11T21:12:18Z")

</div>

Yes but trim() only removes it from the beginning and end of a string. And every blue moon It will miss a '/r' even if its at the end of a string. I've had trim() act funny with some of my returns from the exec node.

lets say I had a string from file like this:

```auto
hello
world

```

I want:

```auto
hello world

```

As a variable msg.payload with no line break or return in the middle or the end.  
This is why i posted the replace option

---

<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:** [11 May 2020 21:15 UTC](https://discourse.nodered.org/t/message-payload-string/26494/11 "2020-05-11T21:15:27Z")

</div>

> [@JGKK](#):
>
> this is what trim() does, no need for a regex replace.

I didn't mean use a regex replace in a function node, I was saying to use a regex match in the switch node so you don't need the function node at all.

---

<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:** [10 July 2020 21:15 UTC](https://discourse.nodered.org/t/message-payload-string/26494/12 "2020-07-10T21:15:27Z")

</div>

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