zemirco / json2csv

Convert json to csv with column titles
http://zemirco.github.io/json2csv
MIT License
2.71k stars 365 forks source link

Problem with double quotes #121

Closed Skuriles closed 6 years ago

Skuriles commented 8 years ago

Hello,

just noticed that the created csv has some problems with "".

Current code (working perfect!!)

 var fieldNames = ['Projekt', 'Start', 'Stop'];
        var fields = ['projectName', 'startDate', 'stopDate'];
        var opts = {
            data: result,
            fields: fields,
            fieldNames: fieldNames,
            quotes: ''
        };

CSV after fs.writefile and download it: Projekt,Start,Stop Projekt,15.06.2016, 13:42,15.06.2016, 13:43 Projekt2,15.06.2016, 13:45,15.06.2016, 13:45 Projekt3,15.06.2016, 13:46,15.06.2016, 14:14 Projekt4,15.06.2016, 14:55,15.06.2016, 14:57 Projekt5,16.06.2016, 10:48,16.06.2016, 10:48

If I remove quotes: ' ' in options (so default will be double quotes") my output will look like this Projekt,"Start","Stop" Projekt,15.06.2016, 13:42,15.06.2016, 13:43 Projekt2,15.06.2016, 13:45,15.06.2016, 13:45 Projekt3,15.06.2016, 13:46,15.06.2016, 14:14 Projekt4,15.06.2016, 14:55,15.06.2016, 14:57 Projekt5,16.06.2016, 10:48,16.06.2016, 10:48

Problem might be here, in the output of

jsonToCsv(opts,
            function(err, csv) {
                  var test = csv;
}

There is a starting "double" double quotes csv = ""Projekt","Start","Stop" "Projekt","15.06.2016, 13:42","15.06.2016, 13:43" "Projekt2","15.06.2016, 13:45","15.06.2016, 13:45" "Projekt3","15.06.2016, 13:46","15.06.2016, 14:14" "Projekt4","15.06.2016, 14:55","15.06.2016, 14:57" "Projekt5","16....

No problem, as I can use the "quotes" parameter in options which helps.. just want to let you know

Thanks for this tool Bene

knownasilya commented 8 years ago

@Skuriles could you submit a failing test?

havenchyk commented 8 years ago

@knownasilya I have the similar error, I've used the example1 in the browser.

var fields = ['car', 'price', 'color'];
var myCars = [
  {
    "car": "Audi",
    "price": 40000,
    "color": "blue"
  }, {
    "car": "BMW",
    "price": 35000,
    "color": "black"
  }, {
    "car": "Porsche",
    "price": 60000,
    "color": "green"
  }
];

after the json2csv, I'm creating the Blob object from the resulted string and the first item doesn't contain the quotes. I didn't check what was the reason of error, just switched to papaparse.unparse, because of lack of time.

knownasilya commented 8 years ago

Thanks for your input.

Skuriles commented 8 years ago

Still need a failing test? Or is the example code good enough?

knownasilya commented 8 years ago

If you have the time, a failing test would be appreciated!

Skuriles commented 8 years ago

I'm out of office for today... will be at least monday... sorry

ejames17 commented 7 years ago

it works for me setting the quotes option to ''. Thanks @Skuriles

pk-pranshu commented 6 years ago

@knownasilya any update on this issue. I have the same problem

pk-pranshu commented 6 years ago

is there a way no to give any quotes ?? Not single or double quotes. The reason I want that is I so that I can store objects in a column with key-value pairs and they are in {"key ": "value"}. I tried quotes: '' doesn't help as the output is '{key:value}'. If I don't pass the quotes option, I get this "{key:value}". Is there any way to disable any sort of quotes and have no quotes

pk-pranshu commented 6 years ago

Ok. this is how I handled it. give doubleQuotes first and then give quotes. var result = json2csv({ data: jsonMessages, fields: fields, doubleQuotes:'"', quotes: '', del: '\t' });

juanjoDiaz commented 6 years ago

I just tried the example above with the latest code (not released yet): And with quote: '' I get:

car,price,color
Audi,40000,blue
BMW,35000,black
Porsche,60000,green

and without it I get:

"car","price","color"
"Audi",40000,"blue"
"BMW",35000,"black"
"Porsche",60000,"green"

Is that correct? Is there anything I'm missing here?

juanjoDiaz commented 6 years ago

I'll close this for a lack of answer.

Feel free to reopen if you still think that is an issue.