How to Correctly create a csv from an oject of Key/values

Basically i want to make a csv with col1 being the key and col 2 being the value
I have the structure complete but i am having trouble including they key as the name of the value



values
i managed to make it include the key but it would apply "Formato" as key for evey value and i am unsure on why
Thanks in advanced to annyon who can help a begginer

I think that you need an array of objects or an array of arrays and what you have is a simple object.

I can't see from your output which node is creating the object you've listed but if that is the structure just before sending to the csv node, add another function node with something like this:

const out = []

Object.keys(msg.payload).forEach( (key) => {
    out.push( [ key, msg.payload[key] ] )
})

msg.payload = out

return msg

Explained better in next post

{"FORMATO":"Formato","NUMERO DI LOTTO":"TEST_BATCH","INIZIO PRODUZIONE":0,"FINE PRODUZIONE":0,"TUBI DOSATI":0,"TOTALE TUBI BUONI PRODOTTI":0,"TUBI VUOTI SCARTATI PER ALL. TUBO ALTO":0,"TUBI VUOTI SCARTATI PER ALL. ROVESCIATO":0,"VELOCITA' IMPOSTATA MACCHINA":86,"REGOLAZIONE PRESSIONE COLPO ARIA":0.6000000238418579,"ASPIRATORE":true,"ABBINAMENTO MACCHINA A VALLE":2,"ORIENTATORE TUBI":1,"ORIENTATORE-RITARDO STOP":0,"ORIENTATORE 1-GRADI STOP":0,"ORIENTATORE 2-GRADI STOP":0,"CONTROLLO LIVELLO TRAMOGGIA":256,"CONTROLLO CARICO MINIMO TUBI":true,"ORE PARZIALI RUNTIME":0,"MINUTI PARZIALI RUNTIME":0,"RIPRISTINO ORIENTATORE  [inizio/fine]":[110,130],"ARRESTO IN FASE  [inizio/fine]":[320,340],"ESPULSORE TAPPO AVANTI  [inizio/fine]":[140,250],"ARIA DOPO IL DOSAGGIO  [inizio/fine]":[230,255],"APERTURA/CHIUSURA UGELLI  [inizio/fine]":[100,235],"PULIZIA TUBETTO  [inizio/fine]":[150,200],"VUOTO RIBALATORE TUBETTI  [inizio/fine]":[100,260],"DISINNESTO PIATTO  [inizio/fine]":[70,80],"SOFFIO ARIA PIANO INCLINATO  [inizio/fine]":[200,340],"ALZAGUIDA SEPARATORE TUBETTI  [inizio/fine]":[200,320],"SOFFIO ARIA CARICO TUBI NELLE BUSSOLE (SPINGITORE)  [inizio/fine]":[110,160],"CONTRASTO RIBALTATORE TUBI [inizio/fine]":[40,200],"SOFFIO ARIA CARICO TUBI NEL RIBALTATORE [inizio/fine]":[260,320],"ARRESTO TUBETTI SUL RIBALTATORE [inizio/fine]":[90,220]}

My question is why if i i pass through @TotallyInformation 's beautiful code when creating the csv file with the following settings between the values of arrays with lenght > 1 it puts commas instead of semicolons
Screenshot from 2022-07-05 15-06-11
Exaple result of what i mean:

...
SOFFIO ARIA PIANO INCLINATO  [inizio/fine];200,340
...

The issue is excel then sees it as a decimal point insteal of 2 seperate values
thanks again to anyone who can help

You need to tell Excel that it is now a semi-colon separated file and not a comma-separated file. It will then work.

Alternatively, you could expand my code somewhat to deal with array values. When a value is an array, you need to convert it to a string. You could use JSON.stringify for that though I always recommend wrapping JSON statements (stringify/parse) in try/catch blocks to prevent total failure if the function hits a roadblock. They are a little fragile as functions.

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