# Column to row & separated by comma

**URL:** <https://discourse.nodered.org/t/column-to-row-separated-by-comma/57755>\
**Category:** General\
**Created:** [4 February 2022 06:59 UTC](https://discourse.nodered.org/t/column-to-row-separated-by-comma/57755 "2022-02-04T06:59:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sahsha](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sahsha/32/43845_2.png) [@sahsha](https://discourse.nodered.org/u/sahsha)\
**Post date:** [4 February 2022 06:59 UTC](https://discourse.nodered.org/t/column-to-row-separated-by-comma/57755/1 "2022-02-04T06:59:57Z")

</div>

Need help. need to convert column data to row separated by comma in msg.payload.  
I get column data from a SQL table, which is in the form of array then I use a csv node  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/408714f99f6d247ee886bed145c586716ff35dbb.png)  
which converts it into string. But I need a Payload as "M01CN001, M01CN003, M01CN011 ..... so on".  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/0/20993d549adb0c8f80b0897985fa7037332dac39.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:** [4 February 2022 07:23 UTC](https://discourse.nodered.org/t/column-to-row-separated-by-comma/57755/2 "2022-02-04T07:23:40Z")

</div>

You will need to provide a sample set of data direct from the SQL node as we dont know the original format.

Could you share a small sample (e.g. 3 elements of the data array).  
Use the " **copy value**" button in the debug window to ensure you capture the the data in correct format.

---

<div class="post-metadata">

**Author:** ![sahsha](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sahsha/32/43845_2.png) [@sahsha](https://discourse.nodered.org/u/sahsha)\
**Post date:** [4 February 2022 07:26 UTC](https://discourse.nodered.org/t/column-to-row-separated-by-comma/57755/3 "2022-02-04T07:26:39Z")

</div>

Sample data

[{"MACHINE\_CODE":"M01CN001"},{"MACHINE\_CODE":"M01CN003"},{"MACHINE\_CODE":"M01CN011"}]

---

<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:** [4 February 2022 07:46 UTC](https://discourse.nodered.org/t/column-to-row-separated-by-comma/57755/4 "2022-02-04T07:46:54Z")

</div>

Assuming `msg.payload` is the data array, in a function node...

```auto
msg.payload = msg.payload.map(e => e.MACHINE_CODE).join(", ")
return msg;

```

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

---

<div class="post-metadata">

**Author:** ![sahsha](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sahsha/32/43845_2.png) [@sahsha](https://discourse.nodered.org/u/sahsha)\
**Post date:** [4 February 2022 07:49 UTC](https://discourse.nodered.org/t/column-to-row-separated-by-comma/57755/5 "2022-02-04T07:49:28Z")

</div>

Thanks a lot 👍 it works.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b29c959f0a0e6a3ccd6f466645eb72f6b4c7cf82.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:** [4 February 2022 07:58 UTC](https://discourse.nodered.org/t/column-to-row-separated-by-comma/57755/6 "2022-02-04T07:58:12Z")

</div>

NOTE: if you intend on building a proper CSV file & any of your columns have commas, you should probably use the CSV node...

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

Demo flow (import using CTRL+I)

```auto
[{"id":"30121ee119157788","type":"function","z":"85e1791bc0cd6285","name":"","func":"msg.payload = msg.payload.map(e => e.MACHINE_CODE).join(\", \")\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2340,"y":120,"wires":[["9e3a8ea54f01c9c9"]]},{"id":"9e3a8ea54f01c9c9","type":"debug","z":"85e1791bc0cd6285","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2350,"y":180,"wires":[]},{"id":"29254c7c855287d3","type":"function","z":"85e1791bc0cd6285","name":"","func":"msg.payload = msg.payload.map(e => e.MACHINE_CODE)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2360,"y":360,"wires":[["5dca6444d36b023e"]]},{"id":"0924c41f0799c94f","type":"debug","z":"85e1791bc0cd6285","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2370,"y":480,"wires":[]},{"id":"5dca6444d36b023e","type":"csv","z":"85e1791bc0cd6285","name":"","sep":",","hdrin":"","hdrout":"none","multi":"one","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":2350,"y":420,"wires":[["0924c41f0799c94f"]]},{"id":"7888ca3f05237050","type":"inject","z":"85e1791bc0cd6285","name":"SQL Data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"MACHINE_CODE\":\"M01CN,001\"},{\"MACHINE_CODE\":\"M01CN,003\"},{\"MACHINE_CODE\":\"M01CN,011\"}]","payloadType":"json","x":2340,"y":300,"wires":[["29254c7c855287d3"]]},{"id":"544ae393bdb9078a","type":"inject","z":"85e1791bc0cd6285","name":"SQL Data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"MACHINE_CODE\":\"M01CN,001\"},{\"MACHINE_CODE\":\"M01CN,003\"},{\"MACHINE_CODE\":\"M01CN,011\"}]","payloadType":"json","x":2320,"y":60,"wires":[["30121ee119157788"]]}]

```

---

<div class="post-metadata">

**Author:** ![sahsha](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sahsha/32/43845_2.png) [@sahsha](https://discourse.nodered.org/u/sahsha)\
**Post date:** [4 February 2022 08:11 UTC](https://discourse.nodered.org/t/column-to-row-separated-by-comma/57755/7 "2022-02-04T08:11:20Z")

</div>

Thanks for the info 👍. I'll keep this in mind.

But for now I only needed the payload string without quotes.

---

<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:** [18 February 2022 08:12 UTC](https://discourse.nodered.org/t/column-to-row-separated-by-comma/57755/8 "2022-02-18T08:12:00Z")

</div>

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