# Convert string in array to value

**URL:** https://discourse.nodered.org/t/convert-string-in-array-to-value/56850
**Category:** General
**Tags:** http-request, database
**Created:** [18 January 2022 11:15 UTC](https://discourse.nodered.org/t/convert-string-in-array-to-value/56850 "2022-01-18T11:15:01Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![WeSleY](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WeSleY](https://discourse.nodered.org/u/WeSleY)
#### Post date: [18 January 2022 11:15 UTC](https://discourse.nodered.org/t/convert-string-in-array-to-value/56850/1 "2022-01-18T11:15:01Z")

</div>

Hello,

I am new to the forum. I'm Wesley, 38 years old and have been working on home automation for a while now. Recently I did an integration with node-red and the PLC. Everything is working well.

Now in Belgium the energy prices are rising enormously, there will be a capacity tariff. This means that you pay for the peaks that you cause.

I have extracted on a site the energy spot price per hour.  
These are in a payload in an array

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/5/f5f51ca108ef65eb63cbc94ec4280e102182c616.png)

These values are "string" values separated by a comma. I have been searching for a week to convert these values to numbers. What have I already tried:

msg.payload = Number(msg.payload);  
msg.payload = Number(msg.payload.vars["1"].value)  
msg.payload = payload.replace(",", ".");

Nothing works ☹  
(error NaN (not a number))

I have a value per hour, from this I want:  
1: the average  
2: the 5 highest values (hours are available in topic)

All thanks for the help

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [18 January 2022 11:31 UTC](https://discourse.nodered.org/t/convert-string-in-array-to-value/56850/2 "2022-01-18T11:31:21Z")

</div>

Hello Wesley,

your msg.payload is an array so in order to pick one element of that array and convert its nested payload to a number you have to do .. for example :

`msg.payload = Number(msg.payload[1].payload) `

the above is for converting the string of element in position 1

if you want to convert all your strings then :

`msg.payload = msg.payload.map(el => Number(el.payload))`

---

<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: [18 January 2022 11:31 UTC](https://discourse.nodered.org/t/convert-string-in-array-to-value/56850/3 "2022-01-18T11:31:38Z")

</div>

Your variable is in `msg.payload[1]. payload`

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

---

<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: [18 January 2022 11:34 UTC](https://discourse.nodered.org/t/convert-string-in-array-to-value/56850/4 "2022-01-18T11:34:14Z")

</div>

> [@UnborN](#):
>
> if you want to convert all your strings then :
> 
> `msg.payload = msg.payload.map(el => Number(el.payload))`

(Off the top of my head)...

I suspect the value will need to have the comma changed to a decimal point

```auto
msg.payload = msg.payload.map(el => Number(el.payload.replace(",",".")))

```

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [18 January 2022 11:41 UTC](https://discourse.nodered.org/t/convert-string-in-array-to-value/56850/5 "2022-01-18T11:41:46Z")

</div>

opps .. missed that part .. you are right

---

<div class="post-metadata">

### Author: ![WeSleY](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WeSleY](https://discourse.nodered.org/u/WeSleY)
#### Post date: [18 January 2022 11:43 UTC](https://discourse.nodered.org/t/convert-string-in-array-to-value/56850/6 "2022-01-18T11:43:58Z")

</div>

Hello,

Thanks in advance for helping me think.

If I use  
msg.payload = msg.payload.map(el =\> Number(el.payload))  
then

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/53c247b75e34e03e6a04cfcf1a370df6838e63ec.png)

do i use the "replace" then  
TypeError: Cannot read property 'replace' of undefined

I collect the data as follows:

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

Thanks

---

<div class="post-metadata">

### Author: ![WeSleY](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WeSleY](https://discourse.nodered.org/u/WeSleY)
#### Post date: [18 January 2022 11:50 UTC](https://discourse.nodered.org/t/convert-string-in-array-to-value/56850/7 "2022-01-18T11:50:53Z")

</div>

> [@Steve-Mcl](#):
>
> `msg.payload = msg.payload.map(el => Number(el.payload.replace(",",".")))`

after Steve's post, I adjusted the payload

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/a/1a84e300ce4f9dce1c0f0d5792ef1f0cb9dcc8e2.png)

---

<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: [18 January 2022 11:53 UTC](https://discourse.nodered.org/t/convert-string-in-array-to-value/56850/8 "2022-01-18T11:53:49Z")

</div>

one of your values will be invalid.

Proof

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

when the payload contains `[{ payload: '1,2' }, { payload: '3,4' }, { payload: null }]` you get error `Cannot read property 'replace' of null`

You could filter the array first

```auto
const filtered = msg.payload.filter(e => (!!e && e.payload != null))
const converted = filtered.map(el => Number((el.payload + "").replace(",",".")))
msg.payload = converted;
return msg;

```

EDIT: updated ↑ function to handle situation when payload is already a number

---

<div class="post-metadata">

### Author: ![WeSleY](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WeSleY](https://discourse.nodered.org/u/WeSleY)
#### Post date: [18 January 2022 13:15 UTC](https://discourse.nodered.org/t/convert-string-in-array-to-value/56850/9 "2022-01-18T13:15:23Z")

</div>

If I retrieve everything without trying to convert I have the values below:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/e/eedbbccc727ae0cabc072f9834156c12d91fdb6f.png)  
I may be wrong (don't have much node red experience yet) but I don't think I have any invalid values?

if i filter the array:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/e/8e68b40e9c13a4367a24f2e3c027888fc01fafdd.png)

---

<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: [18 January 2022 13:22 UTC](https://discourse.nodered.org/t/convert-string-in-array-to-value/56850/10 "2022-01-18T13:22:43Z")

</div>

You have changed the data format. The code we offered won't work now.

Change code to...

```auto
const filtered = msg.payload.filter(e => e != null)
const converted = filtered.map(el => Number((el + "").replace(",",".")))
msg.payload = converted;
return msg;

```

---

<div class="post-metadata">

### Author: ![WeSleY](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WeSleY](https://discourse.nodered.org/u/WeSleY)
#### Post date: [18 January 2022 13:37 UTC](https://discourse.nodered.org/t/convert-string-in-array-to-value/56850/11 "2022-01-18T13:37:43Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> const filtered = msg.payload.filter(e => e != null)
> const converted = filtered.map(el => Number((el + "").replace(",",".")))
> msg.payload = converted;
> return msg;
> 
> ```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/3/137bef47fd690d65638afdaeead7719919d638a4.png)

No succes ☹

---

<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: [18 January 2022 13:39 UTC](https://discourse.nodered.org/t/convert-string-in-array-to-value/56850/12 "2022-01-18T13:39:54Z")

</div>

Ok copy and paste the payload (of good data) using the copy value button that appears under your mouse when you hover over the payload in the debug window.

surround your data with three backticks to prevent corruption...  
`````  
`like this`  
`````

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [18 January 2022 13:45 UTC](https://discourse.nodered.org/t/convert-string-in-array-to-value/56850/13 "2022-01-18T13:45:06Z")

</div>

We are only seeing part of your Function node code with that screenshot ..  
did you change msg.payload further up or is msg.payload arriving at this Function as you shared in your 1st post ?

can you share the Flow instead of images ?

---

<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: [18 January 2022 13:50 UTC](https://discourse.nodered.org/t/convert-string-in-array-to-value/56850/14 "2022-01-18T13:50:03Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> const filtered = msg.payload.filter(e => (!!e && e.payload != null))
> const converted = filtered.map(el => Number((el.payload + "").replace(",",".")))
> msg.payload = converted;
> return msg;
> 
> ```
> 
> EDIT: updated ↑ function to handle situation when payload is already a number

Here is proof if you send an array of strings in `msg.payload` it does work...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/e/2e1a90dc389945d5fcbad3b1897133bf13502adc.png)

and here is proof in node-red...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/4/44468eed6ddd859fcc82052ec6547c61a3d9bd05.png)

Demo flow...

```auto
[{"id":"3dc90c37ee504bc4","type":"function","z":"eb81e71f9edd709c","name":"filter and convert","func":"\nconst filtered = msg.payload.filter(e => e != null)\nconst converted = filtered.map(el => Number((el + \"\").replace(\",\", \".\")))\nmsg.payload = converted;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1700,"y":400,"wires":[["47f532c32e1e1cc0"]]},{"id":"f8a71f9cef66afb7","type":"inject","z":"eb81e71f9edd709c","name":"your data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"192,36\",\"225,00\",\"213,72\"]","payloadType":"json","x":1480,"y":400,"wires":[["3dc90c37ee504bc4","96e556112cd78945"]]},{"id":"96e556112cd78945","type":"debug","z":"eb81e71f9edd709c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1560,"y":440,"wires":[]},{"id":"47f532c32e1e1cc0","type":"debug","z":"eb81e71f9edd709c","name":"result","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1850,"y":440,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![WeSleY](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WeSleY](https://discourse.nodered.org/u/WeSleY)
#### Post date: [18 January 2022 14:13 UTC](https://discourse.nodered.org/t/convert-string-in-array-to-value/56850/15 "2022-01-18T14:13:28Z")

</div>

Hello,

Verry strange, now it's working 😃  
thanks a lot!!!!!

---

<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: [1 February 2022 14:13 UTC](https://discourse.nodered.org/t/convert-string-in-array-to-value/56850/16 "2022-02-01T14:13:49Z")

</div>

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