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

**URL:** <https://discourse.nodered.org/t/how-to-correctly-create-a-csv-from-an-oject-of-key-values/64686>\
**Category:** General\
**Created:** [4 July 2022 14:49 UTC](https://discourse.nodered.org/t/how-to-correctly-create-a-csv-from-an-oject-of-key-values/64686 "2022-07-04T14:49:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nenny](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nenny/32/64086_2.png) [@nenny](https://discourse.nodered.org/u/nenny)\
**Post date:** [4 July 2022 14:49 UTC](https://discourse.nodered.org/t/how-to-correctly-create-a-csv-from-an-oject-of-key-values/64686/1 "2022-07-04T14:49:37Z")

</div>

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

 ![Flow struct](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/8/a8fa2b533afe220f8904978f5351dbc4d377ad4e.png)  
 ![structure](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/2/d29ff646122aa2e2e93edcbfacb0fdeef4b342a8.png)  
 ![values](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/400f87a8e116848518fc01f3cb522c6fdc5ee56a.png)  
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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 July 2022 18:59 UTC](https://discourse.nodered.org/t/how-to-correctly-create-a-csv-from-an-oject-of-key-values/64686/2 "2022-07-04T18:59:58Z")

</div>

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:

```auto
const out = []

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

msg.payload = out

return msg

```

---

<div class="post-metadata">

**Author:** ![nenny](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nenny/32/64086_2.png) [@nenny](https://discourse.nodered.org/u/nenny)\
**Post date:** [5 July 2022 09:27 UTC](https://discourse.nodered.org/t/how-to-correctly-create-a-csv-from-an-oject-of-key-values/64686/3 "2022-07-05T09:27:55Z")

</div>

Explained better in next post

---

<div class="post-metadata">

**Author:** ![nenny](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nenny/32/64086_2.png) [@nenny](https://discourse.nodered.org/u/nenny)\
**Post date:** [5 July 2022 13:08 UTC](https://discourse.nodered.org/t/how-to-correctly-create-a-csv-from-an-oject-of-key-values/64686/4 "2022-07-05T13:08:59Z")

</div>

```auto
{"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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/d/7d55713fdd82fec6fd6f364ba41db9091f0ee22e.png)  
Exaple result of what i mean:

```auto
...
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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [5 July 2022 13:15 UTC](https://discourse.nodered.org/t/how-to-correctly-create-a-csv-from-an-oject-of-key-values/64686/5 "2022-07-05T13:15:41Z")

</div>

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.

---

<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:** [19 July 2022 13:16 UTC](https://discourse.nodered.org/t/how-to-correctly-create-a-csv-from-an-oject-of-key-values/64686/6 "2022-07-19T13:16:35Z")

</div>

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