Getting several values from a string

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.

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

Gives 11, which is as I expect.
But

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

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

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

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

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

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

It works. The output is an object.

I am guessing that

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.

yes,

yes

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

they can be omitted it just habit for me.

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

flow.set("a_name", output);

They would then be available as to call in a function

let var_name = flow.get("a_name")

Then you can reference them

var_name.A00

parts is declared by the forEach function.

...forEach(parts => {.....
1 Like

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 非常感謝

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

It says

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

And it will only get the first variable out.

I replaced it with

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

That is a typo, it should be $!

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.

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