How to create csv/excel from object names and values

I know this will be simple but I don't know how to do it.

I have a payload that is a list of objects and values. There are a variable number of such objects so it could be 1 to many.

an example of the data is below:

{"node-red-contrib-cifs":"~0.0.3","node-red-contrib-filter":"~0.1.4","node-red-contrib-float":"~1.0.3","node-red-contrib-fs-ops":"~1.6.0","node-red-contrib-ftp":"~0.0.6","node-red-contrib-mios":"~0.2.0","node-red-contrib-rsync":"~1.1.1","node-red-contrib-smb":"~1.2.0"}

All I want to do is create a csv/excel file of this list with the headers of "Package" for the object names and "Version" for the value.

2023-08-11

Thought I could use the csv node but that seems to create the object name as a column, not a csv value.

What nodes should I be using?

The CSV node - but it expects your data to be in a useable format.

Here I use a function node to convert your data into an array of objects before passing it to the CSV node

Demo flow: (use CTRL-I to import)

[{"id":"0a15c51a2cde6325","type":"inject","z":"1ca1817447127d22","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1370,"y":280,"wires":[["0b81a43b48471e6c"]]},{"id":"0b81a43b48471e6c","type":"template","z":"1ca1817447127d22","name":"your json data","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"{\n    \"node-red-contrib-cifs\": \"~0.0.3\",\n    \"node-red-contrib-filter\": \"~0.1.4\",\n    \"node-red-contrib-float\": \"~1.0.3\",\n    \"node-red-contrib-fs-ops\": \"~1.6.0\",\n    \"node-red-contrib-ftp\": \"~0.0.6\",\n    \"node-red-contrib-mios\": \"~0.2.0\",\n    \"node-red-contrib-rsync\": \"~1.1.1\",\n    \"node-red-contrib-smb\": \"~1.2.0\"\n}","output":"json","x":1540,"y":280,"wires":[["969e27d8733f8c36","7be7fd464a8b0389"]]},{"id":"f3bf04b0a7e76849","type":"debug","z":"1ca1817447127d22","name":"CSV with headers","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1770,"y":400,"wires":[]},{"id":"969e27d8733f8c36","type":"function","z":"1ca1817447127d22","name":"convert to array of objects","func":"msg.payload = Object.entries(msg.payload).map(([Package,Version]) => {\n    return { Package, Version }\n})\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1510,"y":340,"wires":[["a5494b68062b2d63","20b967afc9db667d"]]},{"id":"a5494b68062b2d63","type":"csv","z":"1ca1817447127d22","name":"","sep":",","hdrin":"","hdrout":"all","multi":"one","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":1570,"y":400,"wires":[["f3bf04b0a7e76849"]]},{"id":"7be7fd464a8b0389","type":"debug","z":"1ca1817447127d22","name":"Input Data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1750,"y":280,"wires":[]},{"id":"20b967afc9db667d","type":"debug","z":"1ca1817447127d22","name":"Processed data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1760,"y":340,"wires":[]}]
2 Likes

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