zemirco / json2csv

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

Extra space character is added when using withBOM: true #190

Closed daniel-zahariev closed 6 years ago

daniel-zahariev commented 7 years ago

When adding BOM to the CSV output there's an extra space character. This makes Excel show the first cell with quotes.

Code sample

var json2csv = require('json2csv');
var fs = require('fs');
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"
  }
];
var csv = json2csv({ data: myCars, fields: fields, withBOM: true });

fs.writeFile('file.csv', csv, function(err) {
  if (err) throw err;
  console.log('file saved');
});

CSV output

 "car","price","color"
"Audi",40000,"blue"
"BMW",35000,"black"
"Porsche",60000,"green"
patresk commented 7 years ago

+1

knownasilya commented 7 years ago

Would the fix be as simple as removing the space on that line? Or would that break the BOM?

daniel-zahariev commented 7 years ago

@knownasilya removing the space won't break the BOM and it's as simple as just deleting it from this string.

knownasilya commented 7 years ago

I guess the question is, why was it not caught in the test, https://github.com/zemirco/json2csv/blob/master/test/index.js#L720

Feel free to submit a PR with modified test

juanjoDiaz commented 6 years ago

Any reason why this is still open?

knownasilya commented 6 years ago

Good catch