mholt / PapaParse

Fast and powerful CSV (delimited text) parser that gracefully handles large files and malformed input
http://PapaParse.com
MIT License
12.3k stars 1.14k forks source link

transformHeader for unparse? #1036

Open MihailsKuzmins opened 5 months ago

MihailsKuzmins commented 5 months ago

According to the documentation (https://www.papaparse.com/docs#unparse-config-default) unparse does not provide "transformHeader" functionality, so it is not possible to customize the headers while preserving the original object. Are there any plans to add this functionality in the future?

For now the only solution is to add these original properties to the original object and specify them as "columns" of UnparseConfig.

const csvOptions: UnparseConfig = {
  columns: ['customHeader'],
  // ... more properties
};

for (const item of items) {
  item['customHeader'] = item['realProperty'];
}

const csvData = Papa.unparse(items, csvOptions);
vadimcoder commented 1 month ago

You can achieve this with https://github.com/juanjoDiaz/json2csv

    const data = [
      { "carModel": "Audi", "price": 0, "color": "blue" },
      { "carModel": "BMW", "price": 15000, "color": "red", "manual": true },
      { "carModel": "Mercedes", "price": 20000, "color": "yellow" },
      { "carModel": "Porsche", "price": 30000, "color": "green" }
    ];

    const opts = {
      fields: [
        {
          label: 'Car Model',
          value: 'carModel'
        },
        {
          label: 'Price',
          value: "price",
        },
        {
          label: 'Color',
          value: 'color'
        },
        {
          label: 'Manual',
          value: 'manual',
          default: false
        }
      ]
    };

    const json2csvParser = new Parser();
    const csv = json2csvParser.parse(data);