zemirco / json2csv

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

fields only works if it's the same as the property names... #163

Closed Neats29 closed 7 years ago

Neats29 commented 7 years ago

Thanks for creating this module. I thought that the role of the fields option is so that you can give different names to the headings but it doesn't work if the field names don't match the property names. For example:

var fields = ['cars', 'prices', 'colors'];
var myCars = [
  {
    "car": "Audi",
    "price": 40000,
    "color": "blue"
  }, {
    "car": "BMW",
    "price": 35000,
    "color": "black"
  }, {
    "car": "Porsche",
    "price": 60000,
    "color": "green"
  }
];
json2csv({ data: myCars, fields: fields }, (err, csv) => console.log(csv));

returns:
"cars","prices","colors"
 ,,
 ,,
 ,,

Is this the expected behaviour? Seems a bit odd that you wouldn't be able to change the names if there is a fields option.

knownasilya commented 7 years ago

You must use the object format of fields, see https://github.com/zemirco/json2csv#example-fields-option

Neats29 commented 7 years ago

@knownasilya that worked, thank you 👍