# Object export problem in CSV

**URL:** <https://discourse.nodered.org/t/object-export-problem-in-csv/82693>\
**Category:** General\
**Created:** [7 November 2023 17:51 UTC](https://discourse.nodered.org/t/object-export-problem-in-csv/82693 "2023-11-07T17:51:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![JeromeVISUALS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeromevisuals/32/40103_2.png) [@JeromeVISUALS](https://discourse.nodered.org/u/JeromeVISUALS)\
**Post date:** [7 November 2023 17:51 UTC](https://discourse.nodered.org/t/object-export-problem-in-csv/82693/1 "2023-11-07T17:51:34Z")

</div>

Hi, me again, coming with a small problem concerning CSV export 🙂

Most of the Array I export to CSV using the default CSV node is output correctly, but I have a trouble with objects in the array :

here is the object content . I tried different methods but no one work :

```auto
var csv = '{"desc":"","label":"123_CH07_RxA04"}'
csv = JSON.parse(csv)

```

and the result in CSV export :

```auto
descriptor
"{""desc"":"""",""label"":""123_CH07_RxA04""}"

```

How can I get rid in a proper way of the extra """ , which are not supposed to be there ?

Thanks

Jerome

---

<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:** [7 November 2023 20:13 UTC](https://discourse.nodered.org/t/object-export-problem-in-csv/82693/2 "2023-11-07T20:13:52Z")

</div>

> [@JeromeVISUALS](#):
>
> ```auto
> var csv = '{"desc":"","label":"123_CH07_RxA04"}'
> csv = JSON.parse(csv)
> 
> ```

It is best to use variable names that have correct meaning otherwise you will always get confused or someone trying to read your code will (both probably).

So your first assignment (which should use `let` not `var` by the way) is a string. So it should be something like this:

```auto
let myJson = '{"desc":"","label":"123_CH07_RxA04"}'
try {
   myJson = JSON.parse(myJson)
} catch (e) {
  // Do sometihng if needed if the parse fails - JSON.parse will ALWAYS occasionally fail, bet on it
}

```

Which makes more sense and `myJson` now actually contains a JS object as expected.

However, not sure why you want to turn that string into an object and then write it to CSV, that will likely always be an issue. You WANT your JSON to be a string before trying to write it to a CSV. So if anything you should be doing all this the other way around. Start with the JS object, use `JSON.stringify` (which also needs a try/catch around it) and then write that to CSV.

---

<div class="post-metadata">

**Author:** ![JeromeVISUALS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeromevisuals/32/40103_2.png) [@JeromeVISUALS](https://discourse.nodered.org/u/JeromeVISUALS)\
**Post date:** [8 November 2023 08:33 UTC](https://discourse.nodered.org/t/object-export-problem-in-csv/82693/3 "2023-11-08T08:33:32Z")

</div>

Hi, thanks for your reply, and sorry for the var/let mistake, I'm still learning 🙂

I adapted the quote a little bit following your advices, but still no luck.

Actually, indeed it's fine by using only the string , it's non-sense to parse then stringify, that was just for testing a few different possibilities.

so, when I output the string I get the correct content, however, to get it in a CSV file, I need to send a Array with objects, like this :

```auto
let descriptorContent = '{"desc":"","label":"123_CH07_RxA04"}'
//csv = JSON.parse(csv)

        //csv = JSON.stringify(csv)
        var csvArray=[{"descriptor": csv}]
        //csv = (csv).replace('"')
   
msg.payload = csvArray;

return msg;

```

Then I get the proper column name (descriptor), but the CSV content is not good :

```auto
descriptor
"{""desc"":"""",""label"":""123_CH07_RxA04""}"

```

Thanks in advance,

Jerome

---

<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:** [8 November 2023 13:26 UTC](https://discourse.nodered.org/t/object-export-problem-in-csv/82693/4 "2023-11-08T13:26:25Z")

</div>

So what I did was to create a workbook and save as CSV:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/34fdfeec828d4b26c7bb8184dada093ee1a5f93a.png)

Opening the CSV as raw in VSCode which shows this:

```auto
Col1,Col2
"{""desc"":"""",""label"":""123_CH07_RxA04""}","{""desc"":"""",""label"":""123_CH07_RxA04""}"
"{""desc"":"""",""label"":""123_CH07_RxA04""}","{""desc"":"""",""label"":""123_CH07_RxA04""}"

```

So as you can see, the result you are getting is absolutely correct.

---

<div class="post-metadata">

**Author:** ![JeromeVISUALS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeromevisuals/32/40103_2.png) [@JeromeVISUALS](https://discourse.nodered.org/u/JeromeVISUALS)\
**Post date:** [8 November 2023 14:49 UTC](https://discourse.nodered.org/t/object-export-problem-in-csv/82693/5 "2023-11-08T14:49:25Z")

</div>

Yes, but I'd like to get the exact same content is what we can see in the original string...

I could get rid of the " " " from the string, but I need in the end also "" when there is no value for any of the key ( as we can see in desc in this specific case...)

Don't know how I can handle this, if I use just a space in desc then I'd get a " " in the final csv....

---

<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:** [8 November 2023 17:16 UTC](https://discourse.nodered.org/t/object-export-problem-in-csv/82693/6 "2023-11-08T17:16:41Z")

</div>

Sorry, I'm lost now. A CSV is a defined schema and complex strings HAVE to be escaped otherwise you don't have a working CSV. The double double-quotes are REQUIRED in the CSV. Without them you cannot use the CSV anywhere, it won't work.

To prove the case, try with the quotes in place and then manually edit one of the entries. In both cases, open the csv in Excel or similar and see what happens.

---

<div class="post-metadata">

**Author:** ![JeromeVISUALS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeromevisuals/32/40103_2.png) [@JeromeVISUALS](https://discourse.nodered.org/u/JeromeVISUALS)\
**Post date:** [8 November 2023 19:25 UTC](https://discourse.nodered.org/t/object-export-problem-in-csv/82693/7 "2023-11-08T19:25:11Z")

</div>

Hi, so I'm lost too.

Here is a snippest of the csv(-like ?) export I get from my system in a txt file, and from which I want to manipulate datas.

```auto
control	custom	descriptor	extraAlertFilters	Factory label	isIgmpSource
off	{}	{"desc":"","label":""}	[]	receiver_s06_a03	FALSE
off	{}	{"desc":"blabla","label":""}	[]	receiver_s05_a01	FALSE

```

When I load this one in Excel, I get it right :

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/2/921e5c43d9b544cb371e72e41674ca5c337590f0.png)

And the csv import in NR is fine as well .

So on the input side all looks good to me, but I can't get it exported correctly.....

---

<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:** [8 November 2023 21:55 UTC](https://discourse.nodered.org/t/object-export-problem-in-csv/82693/8 "2023-11-08T21:55:12Z")

</div>

> [@JeromeVISUALS](#):
>
> export I get from my system in a txt file

That is not a CSV. It looks like a TSV (Tab separated). That would actually be easier to work with for your data probably. But it isn't a CSV. You can work with that in Node-RED I think - still using the incorrectly named "CSV" node, just set it to use tabs instead of comma's as the separator. TSV does not need the double quoting as long as you don't try to include tabs in your fields.

---

<div class="post-metadata">

**Author:** ![JeromeVISUALS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeromevisuals/32/40103_2.png) [@JeromeVISUALS](https://discourse.nodered.org/u/JeromeVISUALS)\
**Post date:** [8 November 2023 22:20 UTC](https://discourse.nodered.org/t/object-export-problem-in-csv/82693/9 "2023-11-08T22:20:47Z")

</div>

this is part of the source file, you think it's TSV ?

#;active;bidirPartnerId;codecFormat;configPriority;control;custom;descriptor;extraAlertFilters;Factory label;isIgmpSource;mainDstIp;mainDstMac;mainDstPort;mainDstVlan;mainSrcGateway;mainSrcIp;mainSrcMac;mainSrcNetmask;public;sdpSupport;sipsMode;spareDstIp;spareDstMac;spareDstPort;spareDstVlan;spareSrcGateway;spareSrcIp;spareSrcMac;spareSrcNetmask;tags;useAsEndpoint  
device73.484735f6-a1ea-596e-8282-0cd1108e6be2.R01a52f67-554c-5791-8503-fcbee07f93d9;true;null;Audio;off;off;{};{"desc":"","label":""};;receiver\_s06\_a03;false;null;null;null;null;null;null;null;null;false;true;NONE;null;null;null;null;null;null;null;null;;false  
device73.484735f6-a1ea-596e-8282-0cd1108e6be2.R02d8318e-62ea-5d74-8693-9d3e7024f560;true;null;Audio;off;off;{};{"desc":"blabla","label":""};;receiver\_s05\_a01;false;null;null;null;null;null;null;null;null;false;true;NONE;null;null;null;null;null;null;null;null;;false  
device73.484735f6-a1ea-596e-8282-0cd1108e6be2.R030c78b8-e95c-5012-bfc8-bd7ef3188d16;true;null;Ancillary;off;off;{};{"desc":"","label":""};;receiver\_s02\_m00;false;null;null;null;null;null;null;null;null;false;true;NONE;null;null;null;null;null;null;null;null;;false  
device73.484735f6-

---

<div class="post-metadata">

**Author:** ![JeromeVISUALS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeromevisuals/32/40103_2.png) [@JeromeVISUALS](https://discourse.nodered.org/u/JeromeVISUALS)\
**Post date:** [8 November 2023 22:41 UTC](https://discourse.nodered.org/t/object-export-problem-in-csv/82693/10 "2023-11-08T22:41:03Z")

</div>

Well, in the end I made a small piece of code by myself in a function node directly connected to a file write node, and now gets the proper result in the target file :

```auto
const separator = ";";

let csvlines = ""

var columns = (Object.keys(msg.payload[0]).join(separator));

for (let index = 0; index < msg.payload.length; index++) {

    msg.payload[index].descriptor = JSON.stringify(msg.payload[index].descriptor);
        
    var line = (Object.values(msg.payload[index]).join(separator) + "\n" );
    csvlines = csvlines + line;

    }

//node.warn("csvContent" + csvlines)
msg.columns = columns;
msg.payload = msg.columns + "\n" + csvlines;

return msg;

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/6/96bcfde02d80c0a682901d758213e4bbbb4e9e38.png)

---

<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:** [7 January 2024 22:41 UTC](https://discourse.nodered.org/t/object-export-problem-in-csv/82693/11 "2024-01-07T22:41:40Z")

</div>

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