CSV node - set columns names from outside the node

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:

// 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.

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.

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

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 (?)

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

Thanks!!! :pray:
Really looks like a perfect solution for my case!

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

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.

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