# Getting several values from a string

**URL:** <https://discourse.nodered.org/t/getting-several-values-from-a-string/76242>\
**Category:** General\
**Tags:** javascript\
**Created:** [7 March 2023 14:53 UTC](https://discourse.nodered.org/t/getting-several-values-from-a-string/76242 "2023-03-07T14:53:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![DozoG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dozog/32/77234_2.png) [@DozoG](https://discourse.nodered.org/u/DozoG)\
**Post date:** [7 March 2023 14:53 UTC](https://discourse.nodered.org/t/getting-several-values-from-a-string/76242/1 "2023-03-07T14:53:56Z")

</div>

Hi everyone,

I have a tcp request node that returns a string containing a series of variables-value pairs:

"!A00=0.25$!A04=228.00$!A22=2.00$"

- Each variable-value pair starts with ! and ends with $
- In the example, the server returned 3 variables with 3 values

I am looking to get out all values and store them in local flow-variable. (or perhaps an MQTT message)  
I think it should be possible with a function node by using 'indexOf' or 'Substring' or 'search' or 'slice' on the string. But I either get the "is not a function" error or I only get the length of the string.

Most of what I found online tells me to first stringify the msg.payload , but I think mine already is a string.

```auto
const searchfor = 'A04=';
const found = msg.payload.indexOf(searchfor);
msg.payload = found;

```

Gives 11, which is as I expect.  
But

```auto
var outp = msg.payload.slice(11,15) ;
msg.payload = outp;

```

Gives error : msg.payload.slice is not a function"  
So I force msg.payload to be a String

```auto
var outp = String(msg.payload).slice(11,15) ;
msg.payload = outp;

```

but it gives me an empty string ""

Why does '.IndexOf' work but '.slice' does not ?  
I guess the more basic question is, is there a specific resource for writing Javascript in Node red?  
I am completely new to Javascript.

Thanks, Oscar

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [7 March 2023 15:12 UTC](https://discourse.nodered.org/t/getting-several-values-from-a-string/76242/2 "2023-03-07T15:12:04Z")

</div>

Would probably be easier to spit it on a delimiter. e.g.

```auto
const divider = "$!"
const remove_front = 1;
const remove_rear = -1;
const output = {}
msg.payload.slice(remove_front, remove_rear).split(divider).forEach(parts => {
  parts = parts.split("=");
  output[parts[0]] = parseFloat(parts[1]);
})
msg.payload = output
return msg;

```

output

```auto
{
  "A00": 0.25,
  "A04": 228,
  "A22": 2
}

```

---

<div class="post-metadata">

**Author:** ![DozoG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dozog/32/77234_2.png) [@DozoG](https://discourse.nodered.org/u/DozoG)\
**Post date:** [8 March 2023 05:39 UTC](https://discourse.nodered.org/t/getting-several-values-from-a-string/76242/3 "2023-03-08T05:39:21Z")

</div>

That's a lot of syntax, let me study that for a bit.  
I have used the very impopular 'strtok' in C++, which was mind-tricky to get used to.

If I understand the first glance, you get an array as an output?  
Or is it a struct with name-value parts as [parts[0]] = name and [parts[1]] = value ?

Anyway, I am really impressed by the response. ( Yours, but in general on forums like these)  
Thank you very much

---

<div class="post-metadata">

**Author:** ![DozoG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dozog/32/77234_2.png) [@DozoG](https://discourse.nodered.org/u/DozoG)\
**Post date:** [8 March 2023 07:25 UTC](https://discourse.nodered.org/t/getting-several-values-from-a-string/76242/4 "2023-03-08T07:25:30Z")

</div>

It works. The output is an object.

I am guessing that

```auto
const output = {}

```

is like declaring the variable output as an object ?

The syntax of the operations is still unclear to me. Do all these operations apply front to back?  
Could those have been written as single operations separately?

It seems that the semi-colon is not very strictly observed ( I accidentally left out the one after parts.split("=") and it doesn't complain. l can't omit one like that in C++)  
It also seems I didn't "declare" the variable 'parts' specifically anywhere?  
(so there are some dots under the first time its used saying it is implicitly 'any' type)

So do I now write another function node to assign all those numbers to flow variables for use elsewhere or at another time?  
Ok, I will google a bit first because I think there are more examples dealing with objects than with my specific formatting of the string.  
Like I said, the javascript syntax is confusing to me.  
Thanks again.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [8 March 2023 07:34 UTC](https://discourse.nodered.org/t/getting-several-values-from-a-string/76242/5 "2023-03-08T07:34:46Z")

</div>

> [@DozoG](#):
>
> is like declaring the variable output as an object ?

yes,

> [@DozoG](#):
>
> Could those have been written as single operations separately?

yes

```auto
msg.payload = msg.payload.slice(1,-1);
msg.payload = msg.payload.split("!$");
msg.payload.forEach(.....

```

> [@DozoG](#):
>
> It seems that the semi-colon is not very strictly observed (

they can be omitted it just habit for me.

> [@DozoG](#):
>
> So do I now write another function node to assign all those numbers to flow variables for use elsewhere or at another time?

You could set them as flow variables in one go if you wish, in the same function.

```auto
flow.set("a_name", output);

```

They would then be available as to call in a function

```auto
let var_name = flow.get("a_name")

```

Then you can reference them

```auto
var_name.A00

```

> [@DozoG](#):
>
> t also seems I didn't "declare" the variable 'parts' specifically anywhere?

parts is declared by the forEach function.

```auto
...forEach(parts => {.....

```

---

<div class="post-metadata">

**Author:** ![DozoG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dozog/32/77234_2.png) [@DozoG](https://discourse.nodered.org/u/DozoG)\
**Post date:** [8 March 2023 09:00 UTC](https://discourse.nodered.org/t/getting-several-values-from-a-string/76242/6 "2023-03-08T09:00:16Z")

</div>

I just went to the dentist and in the meantime you have answered all my questions.  
All clear,  
Thanks so much, or as they say here 非常感謝

---

<div class="post-metadata">

**Author:** ![DozoG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dozog/32/77234_2.png) [@DozoG](https://discourse.nodered.org/u/DozoG)\
**Post date:** [10 March 2023 07:15 UTC](https://discourse.nodered.org/t/getting-several-values-from-a-string/76242/7 "2023-03-10T07:15:38Z")

</div>

Its so finicky. 😶  
I replaced your single line function (your first response) to the separate lines answer.

It says

```auto
msg.payload = msg.payload.split("!$");

```

And it will only get the first variable out.

I replaced it with

```auto
msg.payload = msg.payload.split("$!");

```

So just changed the order of the '$' and '!' in the split parameters  
And it spits out the entire object with all name-value pairs.

There are a lot of search results on google for msg.payload. split, but I can't find an actual reference for the function.  
So I just add that for my future reference or for the benefit of anyone else looking.

Thanks

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [10 March 2023 07:23 UTC](https://discourse.nodered.org/t/getting-several-values-from-a-string/76242/8 "2023-03-10T07:23:55Z")

</div>

> [@DozoG](#):
>
> `sg.payload = msg.payload.split("!$");`

That is a typo, it should be $!

> **[String.prototype.split() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)**
>
> The split() method of String values takes a pattern and divides this string into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.

---

<div class="post-metadata">

**Author:** ![DozoG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dozog/32/77234_2.png) [@DozoG](https://discourse.nodered.org/u/DozoG)\
**Post date:** [10 March 2023 07:28 UTC](https://discourse.nodered.org/t/getting-several-values-from-a-string/76242/9 "2023-03-10T07:28:45Z")

</div>

Hahaha, yes, I figured it out.  
(I kinda compare split to strtok (in C++) that I am somewhat familiar with, so I keep running into differences between the languages)  
And thanks for the link to the resource.

---

<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:** [9 May 2023 07:29 UTC](https://discourse.nodered.org/t/getting-several-values-from-a-string/76242/10 "2023-05-09T07:29:37Z")

</div>

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