# Help with how to parse this message

**URL:** https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817
**Category:** General
**Created:** [21 August 2020 10:08 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817 "2020-08-21T10:08:24Z")
**Posts on this page:** 20
**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 August 2020 10:08 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/1 "2020-08-21T10:08:24Z")

</div>

This is the message:

`rgb,3,0,20,0`

And I want to _switch_ the message depending on the (in this case) `3`.  
It can be 1, 2 or 3.

Thanks in advance.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [21 August 2020 10:09 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/2 "2020-08-21T10:09:36Z")

</div>

You could use a function node and use the split function then set the payload to element [1]

Or use a switch node and test the payload contains `rgb,1,` or `rgb,2,` etc

---

<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 August 2020 10:12 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/3 "2020-08-21T10:12:28Z")

</div>

errrr... I have never heard of that idea.

Is it ....

(After you edit)

Yeah. That would work.

Next part - sorry I didin't include it in the question:  
How to get the last part:  
(eg)`0,20,0`

I am wanting to send that to a node and use that to set the colour.  
So I will also need to be told how to do that.

I think it is `#(numbers)` but am not too sure.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [21 August 2020 10:18 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/4 "2020-08-21T10:18:13Z")

</div>

You really should say in you post what you need (it changes the solution)

So, I would, in a function node, call `.split()` to break up that string by comma, pop the first element off (lose the rgb part) then pop the next element (get the 1,2,3 part). The remaining 3 elements are the bit you are after - you can then call `.join()` to put these back together and send as a payload.

Give it a go (search the net for js string split, is array pop and js string join for clues)

I'll be back at a computer in 10 mins to help out if need be.

---

<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 August 2020 10:20 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/5 "2020-08-21T10:20:34Z")

</div>

Thanks. Shall try.

I tried the `element[1]` thing, but it didn't like me. Yeah, I probably made a mistake.

Thanks.

I'll start work on it.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [21 August 2020 10:21 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/6 "2020-08-21T10:21:24Z")

</div>

You might also want to look into regex.

---

<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 August 2020 10:21 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/7 "2020-08-21T10:21:25Z")

</div>

Where is that coming from? Perhaps it could be converted to a js object earlier in the flow.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [21 August 2020 10:24 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/8 "2020-08-21T10:24:13Z")

</div>

Oooh. I wasn't heading down that rabbit hole.

Only joking Andrew - I couldn't resist. No offence intended.

---

<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 August 2020 10:24 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/9 "2020-08-21T10:24:50Z")

</div>

The message is coming from a device and it is used to .......

Hey, yeah! Silly me. Earlier on in the flow it has names.....

Yeah, that may work.

(D'oh!)

---

<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 August 2020 10:26 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/10 "2020-08-21T10:26:15Z")

</div>

No problems. None taken.

I deserve it in ways.

I have a whole lot of things on my plate and I probably shouldn't keep putting more things on until I get the remainder resolved.

Alas life doesn't always work like that.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [21 August 2020 10:41 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/11 "2020-08-21T10:41:53Z")

</div>

> [@Trying\_to\_learn](#):
>
> Hey, yeah! Silly me. Earlier on in the flow it has names.....
> 
> Yeah, that may work

All's well that ends well Andrew. 👍

* * *

In case you were curious (or want to keep this as a future snippet for when you dont have control over the incoming data format)...

```auto
var spl = msg.payload.split(",");
var rgbPart = spl[0]; //get the rgb part (element 0)
var idPart = spl[1]; //get the id part (element 1)
var rgb = spl.slice(2); //get remaining parts (elements 2 ~ the end)
var rgbStr = rgb.join(","); // join these last three parts by comma back to a string

//set the payload as required - example...
msg.payload = {
  id: idPart,
  rgb: rgb,
  rgbStr: rgbStr
}
return msg;

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/7/97f483a7436f1b4ddd0102b680d87727b2f9960f.png)

---

<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 August 2020 10:50 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/12 "2020-08-21T10:50:07Z")

</div>

Hang on! Something is not right.

Argh! Silly me.

Ok, I got confused. (as per normal.)  
(I think it is getting worse)

This is an example incoming message:  
`{"topic":"POWER/","payload":"REQUEST","qos":0,"retain":false,"_msgid":"f1d1ac73.f1322","device":"TABBYCAT"}`

`payload` and `device` change.

I put that through a `change` node and set message variables.......

Argh! (again: silly me.) I forgot to include the `change` node in the code when testing.

Hang on.

(Anyone got a spare brain?)

---

<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 August 2020 11:01 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/13 "2020-08-21T11:01:12Z")

</div>

Great.

So this is the message:

`{"topic":"POWER/","payload":{"id":"4","rgb":["200","0","0"],"rgbStr":"200,0,0"},"qos":0,"retain":false,"_msgid":"4f32902a.d7c2a","device":"TABBYCAT","REQUEST":"200,0,0","DETECTED":"0,20,0","FULL":"0,255,0","REMOVED":"0,0,0","led":4}`

So I need to send `msg.rgbStr` into a node and use that to set the colour.

So all I need to do in said node is set the `color` to `msg.rgbStr` and then it changes colour to match the message - yes?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [21 August 2020 11:05 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/14 "2020-08-21T11:05:49Z")

</div>

> [@Trying\_to\_learn](#):
>
> So all I need to do in said node is set the `color` to `msg.rgbStr` and then it changes colour to match the message - yes?

Afraid I have no idea what lies beyond that bit of code - but I would if anything it'd be `msg.payload.rgbStr` not `msg.rgbStr`

---

<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 August 2020 11:07 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/15 "2020-08-21T11:07:28Z")

</div>

Oh. Yeah. Oops for me.

I'll deal with the next part. I just wanted to check I am (at this point) on the correct page.

---

<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 August 2020 11:21 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/16 "2020-08-21T11:21:38Z")

</div>

So when I am sending the _colour_ to a node....

The code is (eg:)

`payload:'<font color = "red"> <i class="fa fa-bullseye fa-2x" ></i></font>'`

Ok. So I want to change the _color_ to what the `rgbStr` part is.

I see I can't just do:

`payload:'<font color = ' + rgbStr + '<i calss="fa fa-bullseye fa-2x" ></i></font>',`

Although that is sent _correctly_ the node doesn't understand that.

I need to _qualify_ the `rgbStr` as a rgb code.

That's with a `#` in front of it - yes?  
Oh, and get rid of the `,`. Done. Doesn't seem to work.

I'm missing an elephant somewhere.

This is what I am sending:  
`{"payload":"<font color = #20000<i calss=\"fa fa-bullseye fa-2x\" ></i></font>","device":"TABBYCAT","_msgid":"149286cd.433c19"}`

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [21 August 2020 11:29 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/17 "2020-08-21T11:29:37Z")

</div>

> That's with a `#` in front of it - yes?

only if it is a hex color.

`payload:'<font color = ' + rgbStr + '<i calss="fa fa-bullseye fa-2x" ></i></font>',`

That is incorrectly formatted html and you should send strings as strings.

```auto
{"payload":"<font color='rgb("+rgbStr+")'><i class='fa fa-bullseye fa-2x'></i></font>"}

```

Then you should end up with (if you didnt modify rgbStr):

```auto
{"payload":"<font color='rgb(200,0,0)'><i class='fa fa-bullseye fa-2x'></i></font>"}

```

---

<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 August 2020 11:39 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/18 "2020-08-21T11:39:33Z")

</div>

Thanks @bakman2.

Yes, I really need to learn the structures.

Indulge me.

I copy/pasted the code you gave me.

For clarity the _colour_ I am sending is RGB.  
R 20 G 0 B 0.  
But I'm guessing it wants the numbers to be 00 and 00. So it is causing problems with the colour I am getting.

_Weirdly_ (I say) it isn't the colour I expect. Granted it is missing numbers.  
But it should be some semblance of RED. Yes? No. It is some weird GREEN colour.

me and my stupid ideas to include what is being shown on an LED also on the _Dashboard_.

So how do I make it _format_ the `0` and `00` for the numbers which are less than 10?

Ah! All I need to do is add the extra digits when I define the values.  
(I wonder how the LED is going to deal with this now.)

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [21 August 2020 11:51 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/19 "2020-08-21T11:51:29Z")

</div>

First step is to determine which color format you want to use:

```auto
<font color="rgb(200,0,0)"></font> // rgb format
<font color="argb(200,0,0,0.5)"></font> // argb - alpha channel + rgb
<font color="#ff0000"></font> // hex format
<font color="hsl(89, 43%, 51%)"></font> // hue saturation luminance
<font color="hsla(89, 43%, 51%, 0.6)"></font> // hue saturation luminance + alpha channel

```

If you have RGB available (20,0,0) you can use that directly.

---

<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 August 2020 11:57 UTC](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817/20 "2020-08-21T11:57:40Z")

</div>

Ok.

`rgb(200,0,0)` is good for me.

So I put the `,` back in and I get `200,0,0` sent and that _works_.

I say _works_ because (at time of posting) when I send `0,80,0` I don't see GREEN.  
It sets it initially at RED, but then stops.

Shall go and check the gremlins haven't messed with the code.

Oh! Those values are actually what is in the `rgbStr` part of the message.  
That's the variable. So it can't be fixed numbers.

But the structure is as show. (well for now with extra `0`s.)

Someone shoot me.

Be back shortly.

Right:  
This:  
`{"payload":"<font color='rgb(200,0,0)'><i class='fa fa-bullseye fa-2x'></i></font>","device":"TABBYCAT","_msgid":"917c1fbc.0446"}`

I see `GREEN` (dark).

If I change the message to:

```auto
{"payload":"<font color='rgb(200,0,0)'></font><i class='fa fa-bullseye fa-2x'></i>","device":"TABBYCAT","_msgid":"f635e1e7.6e73c"}

```

The icon is WHITE.

(BTW, how do you get the _better_ format when you post code? I seem to have also forgotten how to do that)

[Next page](https://discourse.nodered.org/t/help-with-how-to-parse-this-message/31817.md?page=2)
