# CSV node - set columns names from outside the node

**URL:** <https://discourse.nodered.org/t/csv-node-set-columns-names-from-outside-the-node/32201>\
**Category:** General\
**Created:** [30 August 2020 05:05 UTC](https://discourse.nodered.org/t/csv-node-set-columns-names-from-outside-the-node/32201 "2020-08-30T05:05:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SergeyBrook](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@SergeyBrook](https://discourse.nodered.org/u/SergeyBrook)\
**Post date:** [30 August 2020 05:05 UTC](https://discourse.nodered.org/t/csv-node-set-columns-names-from-outside-the-node/32201/1 "2020-08-30T05:05:43Z")

</div>

Hello everyone,  
I'm using CSV parser core node to convert CSV to JSON. According to the node's docs, it is possible to send column names list in `msg.columns`, but when i doing so, i just get 'default' column names in resulting JSON (like 'col1', 'col2', ...).  
I tried to examine the node's code (@node-red/nodes/core/parsers/70-CSV.js) and noticed that the branch that handles CSV to JSON conversion, ignores existence of `msg.columns` field.  
My question is this a 'bug' or just not implemented feature?  
In both cases, may i suggest the bug-fix / feature-request for this:

```auto
// Add to '70-CSV.js' to line 163
// Get headers from msg.columns:
 if ((node.hdrin === false) && (node.template.length === 1) && (node.template[0] === '') && (msg.hasOwnProperty("columns"))) {
    node.template = clean((msg.columns || "").split(","));
}

```

Of course, i may not to see the whole picture, so i can miss something...

Thank you,  
Sergey.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [30 August 2020 06:08 UTC](https://discourse.nodered.org/t/csv-node-set-columns-names-from-outside-the-node/32201/2 "2020-08-30T06:08:54Z")

</div>

Reading the node documentation:

> When converting **to CSV** , the column template is used to identify which properties to extract from the object and in what order.

> If the template is blank then the node can use a simple comma separated list of properties supplied in `msg.columns` to determine what to extract. If that is not present then all the object properties are ouput in the order in which they are found.

It seems that it only applies when converting to CSV.

You can use the columns field to define them when converting to an object.

---

<div class="post-metadata">

**Author:** ![SergeyBrook](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@SergeyBrook](https://discourse.nodered.org/u/SergeyBrook)\
**Post date:** [30 August 2020 06:43 UTC](https://discourse.nodered.org/t/csv-node-set-columns-names-from-outside-the-node/32201/3 "2020-08-30T06:43:19Z")

</div>

Thank you for reply - looks like i missed this... 😳  
It is just a bit disappointing that it doesn't work in both ways...

> [@bakman2](#):
>
> You can use the columns field to define them when converting to an object.

You are right - it may be a good solution in case the structure of CSV is always the same, but in my case i'm looking for a way to change the column names dynamically according to different structures i may encounter... Or maybe it would be better for each type of structure create it's own node with static column names (?)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [30 August 2020 06:55 UTC](https://discourse.nodered.org/t/csv-node-set-columns-names-from-outside-the-node/32201/4 "2020-08-30T06:55:49Z")

</div>

What you could do is use a change node, before the csv node and define the columns and prepend it before:

 ![Screenshot 2020-08-30 at 08.54.20](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/2/42eeb19ea1c2c3cd423bc077dec17f51fcaef379.jpeg)

 ![Screenshot 2020-08-30 at 09.00.57](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/d/ed2494fabcb157e11cbf771ee5ccbf3672400cc2.jpeg)

---

<div class="post-metadata">

**Author:** ![SergeyBrook](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@SergeyBrook](https://discourse.nodered.org/u/SergeyBrook)\
**Post date:** [30 August 2020 07:08 UTC](https://discourse.nodered.org/t/csv-node-set-columns-names-from-outside-the-node/32201/5 "2020-08-30T07:08:26Z")

</div>

Thanks!!! 🙏  
Really looks like a perfect solution for my case!

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [30 August 2020 07:13 UTC](https://discourse.nodered.org/t/csv-node-set-columns-names-from-outside-the-node/32201/6 "2020-08-30T07:13:28Z")

</div>

Often CSV files have their columns defined in the first line, which would also have same effect if you had different files arriving.

---

<div class="post-metadata">

**Author:** ![SergeyBrook](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@SergeyBrook](https://discourse.nodered.org/u/SergeyBrook)\
**Post date:** [30 August 2020 07:37 UTC](https://discourse.nodered.org/t/csv-node-set-columns-names-from-outside-the-node/32201/7 "2020-08-30T07:37:25Z")

</div>

Thank you for this point!  
Currently, all the files are coming w/o headers but in few different structures.  
But for more flexibility, it is a good idea to make a check for header existence before prepending the data with my own headers.

---

<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:** [13 September 2020 07:37 UTC](https://discourse.nodered.org/t/csv-node-set-columns-names-from-outside-the-node/32201/8 "2020-09-13T07:37:36Z")

</div>

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