# Convert csv string to array

**URL:** <https://discourse.nodered.org/t/convert-csv-string-to-array/19088>\
**Category:** General\
**Created:** [12 December 2019 21:34 UTC](https://discourse.nodered.org/t/convert-csv-string-to-array/19088 "2019-12-12T21:34:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [12 December 2019 21:34 UTC](https://discourse.nodered.org/t/convert-csv-string-to-array/19088/1 "2019-12-12T21:34:25Z")

</div>

I feel this should be possible using basic nodes but I'm failing to acheive a result

I've got and rgb triple coming in via MQTT as a csv string e.g

255,255,0

but I need to change it to an array to feed the WS2801 node

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [12 December 2019 21:41 UTC](https://discourse.nodered.org/t/convert-csv-string-to-array/19088/2 "2019-12-12T21:41:34Z")

</div>

String method `split()` with separator comma ` split(",")` makes array  
Or with `split` node  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/0b7bc0586ecc4935e9213a805821e9e2dec8bb80.png)

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [12 December 2019 21:47 UTC](https://discourse.nodered.org/t/convert-csv-string-to-array/19088/3 "2019-12-12T21:47:37Z")

</div>

That just seems to give me 3 separate string messages

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [12 December 2019 22:03 UTC](https://discourse.nodered.org/t/convert-csv-string-to-array/19088/4 "2019-12-12T22:03:11Z")

</div>

> [@cymplecy](#):
>
> 255,255,0

Right, but can be fixed by using a join node after the split node, with below config.

![a-1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/9ee064e813ee1b7f4715619505c2d37cb0c67ef1.png)

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [12 December 2019 23:43 UTC](https://discourse.nodered.org/t/convert-csv-string-to-array/19088/5 "2019-12-12T23:43:31Z")

</div>

> [@cymplecy](#):
>
> I've got and rgb triple coming in via MQTT as a csv string e.g
> 
> 255,255,0
> 
> but I need to change it to an array to feed the WS2801 node

Simon, Does this JSONata expression in a `change` node do what you want?

`payload.$split(",").$number()`

_(not sure if you need an array of numbers, or just leave them as strings...)_

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [12 December 2019 23:48 UTC](https://discourse.nodered.org/t/convert-csv-string-to-array/19088/6 "2019-12-12T23:48:12Z")

</div>

Yes it does but I trying to see if a core node would do it - I felt the csv node would do it on its own but it doesn't

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [13 December 2019 00:01 UTC](https://discourse.nodered.org/t/convert-csv-string-to-array/19088/7 "2019-12-13T00:01:28Z")

</div>

Well, of course the `change` and `function` nodes _are_ core... but I get your point.

So here is an alternative solution -- use the `template` node to build an array "string" and output as "parsed JSON"

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

... which only works if you are guaranteed to have input strings of comma-separated numbers.  
In practice, you would probably want to add some kind of error checking.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [13 December 2019 09:44 UTC](https://discourse.nodered.org/t/convert-csv-string-to-array/19088/8 "2019-12-13T09:44:43Z")

</div>

yep \_ I had that one but I think its pretty much the same as using JSONata

Actually - your template looks different to mine #strange

These are the setting I needed to get an array output

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/7781ae5bf36439fb714b003488b503e2d4b30d2e.png)

---

<div class="post-metadata">

**Author:** ![thierry-rhone](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thierry-rhone/32/39551_2.png) [@thierry-rhone](https://discourse.nodered.org/u/thierry-rhone)\
**Post date:** [26 February 2022 20:13 UTC](https://discourse.nodered.org/t/convert-csv-string-to-array/19088/9 "2022-02-26T20:13:34Z")

</div>

Forgive me for digging up this topic.  
I have a csv file

```auto
TF1,1
France 2,2
France 3,3
France 5,5
M6,6
...

```

I would like to convert it to an array:

```auto
msg.payload: array[200]
   0: "TF1","1"
   1: "France 2","2"
   2: "France 3","3"
   3: "France 5","5"
   4: "M6","6"
   ...

```

I tried whith split - join and template node whitout succes.  
Can you help me  
Thanks

---

<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:** [26 February 2022 20:52 UTC](https://discourse.nodered.org/t/convert-csv-string-to-array/19088/10 "2022-02-26T20:52:52Z")

</div>

Have you tried the CSV node?
