CSV node outputs multiple identical lines

I have a problem with the CSV node. In my flow, I pass an array of objects, as the payload, to the CSV node. I expect the node to output a csv string containing a line for each object in the array. The node does output a line for each object, but the data from the first object in the array is repeated in all the lines. I find this very strange.

Here is a simplified version of what I am trying to do:

[{"id":"91798dce.d68c5","type":"inject","z":"d81de417.1351d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":2660,"wires":[["a55a46c8.febe18"]]},{"id":"7f820176.a1cc","type":"debug","z":"d81de417.1351d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":510,"y":2720,"wires":[]},{"id":"df9aac72.cfe4a","type":"debug","z":"d81de417.1351d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":2660,"wires":[]},{"id":"4b5a4d85.2e63e4","type":"csv","z":"d81de417.1351d8","name":"","sep":",","hdrin":"","hdrout":"none","multi":"one","ret":"\\r\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":510,"y":2660,"wires":[["df9aac72.cfe4a"]]},{"id":"a55a46c8.febe18","type":"function","z":"d81de417.1351d8","name":"","func":"msg.payload = []; // create an empty array\nmsg.payload[0] = {\n    \"M1DriveBearingTemp\":21,\n    \"M1NonDriveBearingTemp\":22,\n    \"M1StatorWindingTemp\":23,\n    \"M2DriveBearingTemp\":24,\n    \"M2NonDriveBearingTemp\":25,\n    \"M2StatorWindingTemp\":26\n};\n\nmsg.payload[1] = {\n    \"M1DriveBearingTemp\":31,\n    \"M1NonDriveBearingTemp\":32,\n    \"M1StatorWindingTemp\":33,\n    \"M2DriveBearingTemp\":34,\n    \"M2NonDriveBearingTemp\":35,\n    \"M2StatorWindingTemp\":36\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":2660,"wires":[["4b5a4d85.2e63e4","7f820176.a1cc"]]}]

The picture below shows the debug output before and after the CSV node. As you can see, even though the two objects have different values, the CSV lines are identical and match only the first object.

Any insight would be much appreciated.

Hi Johannes,

this is a unique case .. the CSV Help mentions how the csv node processes the data but it seems that it doesnt fully apply for array of objects without further processing before and after the csv.

I was thinking if you use a Split node before the CSV and a Join node after it maybe you'll get the result you need.

Based on your example :

[{"id":"91798dce.d68c5","type":"inject","z":"c17b9542.d67d68","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":220,"wires":[["a55a46c8.febe18"]]},{"id":"7f820176.a1cc","type":"debug","z":"c17b9542.d67d68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":360,"wires":[]},{"id":"df9aac72.cfe4a","type":"debug","z":"c17b9542.d67d68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1050,"y":220,"wires":[]},{"id":"4b5a4d85.2e63e4","type":"csv","z":"c17b9542.d67d68","name":"","sep":",","hdrin":"","hdrout":"none","multi":"one","ret":"\\r\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":730,"y":220,"wires":[["df1cc12c.bf53c8"]]},{"id":"a55a46c8.febe18","type":"function","z":"c17b9542.d67d68","name":"","func":"msg.payload = []; // create an empty array\nmsg.payload[0] = {\n    \"M1DriveBearingTemp\":21,\n    \"M1NonDriveBearingTemp\":22,\n    \"M1StatorWindingTemp\":23,\n    \"M2DriveBearingTemp\":24,\n    \"M2NonDriveBearingTemp\":25,\n    \"M2StatorWindingTemp\":26\n};\n\nmsg.payload[1] = {\n    \"M1DriveBearingTemp\":31,\n    \"M1NonDriveBearingTemp\":32,\n    \"M1StatorWindingTemp\":33,\n    \"M2DriveBearingTemp\":34,\n    \"M2NonDriveBearingTemp\":35,\n    \"M2StatorWindingTemp\":36\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":380,"y":220,"wires":[["7f820176.a1cc","bbe7861d.1dc018"]]},{"id":"bbe7861d.1dc018","type":"split","z":"c17b9542.d67d68","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":570,"y":220,"wires":[["4b5a4d85.2e63e4"]]},{"id":"df1cc12c.bf53c8","type":"join","z":"c17b9542.d67d68","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\r","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":890,"y":220,"wires":[["df9aac72.cfe4a"]]}]

@JCLeuvennink

Yikes, looks like a real bug - please raise an Issue on the main Node-RED project.
Thanks

Thanks for the workaround, it did the trick! I guess I can also just write a custom function to do the conversion, but this is still easier.

yea .. actually i was experimenting yesterday :wink:
something like this could do the trick :

let arr = msg.payload
let result = '';

arr.forEach(el => {
    result += Object.values(el).join(',') + '\r\n'
})

msg.payload = result;

return msg;

Fix now in master - https://github.com/node-red/node-red/commit/ca4960e097609fdf113d83420a667afbce7f9b6b

1 Like

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