# Splitting data from msg.payload

**URL:** <https://discourse.nodered.org/t/splitting-data-from-msg-payload/14473>\
**Category:** General\
**Created:** [19 August 2019 07:48 UTC](https://discourse.nodered.org/t/splitting-data-from-msg-payload/14473 "2019-08-19T07:48:32Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [19 August 2019 08:53 UTC](https://discourse.nodered.org/t/splitting-data-from-msg-payload/14473/4 "2019-08-19T08:53:42Z")

</div>

Okay, then I'll spoil you with the solution, with an explanation on the how (I'm sick and bored and that's the excuse I'll use for it today :P):  
What you're going to need is a function node, after your serial node. From what I remember from seeing this node, there is a configuration option that lets you trim off the whitespace, by selecting the return type or something like that. If it's there, select `carriage return line feed` or enter `\r\n` if it is an input. If it's not present there, you will have to add it to the function node as well. As for the code, let me show you the input message again, but simplified:

```auto
msg = {
    payload: "PPV\t7\r\n"
}

```

```auto
msg = { 
    payload: "VPV\t35790\r\n"
}

```

These are the two messages you posted in the first post. For the first one you would like the value `7` as output, for the second `35790`. In the current shape, the `msg.payload` is a string. Meaning you can call the `split()` method on it directly. The separator you want to split it on is the tab part of the whitespace, that is the horizontal arrow. In a string that tab is displayed as `\t`. So to split you take `msg.payload.split('\t')` and now you have an array with in the first item the `VPV` or `PPV` string, and in the second the value followed by the ending whitespace. To get rid of that whitespace, you take the value and call `.trim()` on it. You could also flip those around, first calling `.trim()` to get rid of the whitespace at the start and end, then on the result from that split on the tab in the middle.

```auto
var stripped = msg.payload.trim();
var values = stripped.split('\t');
// As you are only appear to be interested in the value, 
// use [1] to access the actual value. The array is zero-indexed, meaning
// that the first item is [0], and the second is thus [1].
msg.payload = values[1];
return msg;

```

If you put that in a function node, it gives you the value, but still as a string. If you need it as a number, you have three options, more or less:

1. for a whole number, an integer: `msg.payload = parseInt(msg.payload, 10); // 10 as in base 10, a decimal number`
2. for a decimal number, or rather a floating point: `msg.payload = parseFloat(msg.payload);`
3. if you don't know if it's a decimal or whole number, or if you want it to fail if there's still text in it: `msg.payload = Number(msg.payload);` If it has any text in it that makes it's unable to get a number, you will get `NaN` instead: Not a Number.

---

_[View the full topic](https://discourse.nodered.org/t/splitting-data-from-msg-payload/14473)._
