zemirco / json2csv

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

Allow empty data set if fields specified #280

Closed joaonice closed 6 years ago

joaonice commented 6 years ago

json2csv version: 4.0.2

Code:

const Json2csvParser = require('json2csv').Parser;
const fields = ['field1', 'field2', 'field3'];
const opts = { fields };

try {
  const parser = new Json2csvParser(opts);
  const csv = parser.parse([]);
  console.log(csv);
} catch (err) {
  console.error(err);
}

Getting the error:

Data should not be empty or the "fields" option should be included

Fields are configured and I'm still getting the error.

knownasilya commented 6 years ago

Looks like we need to tweak that message, but basically it shows up because your data has no items.

https://github.com/zemirco/json2csv/blob/master/lib/JSON2CSVParser.js#L43

Is there a reason you'd want a CSV without any data?

joaonice commented 6 years ago

In my code, in some situations, my empty state data is an empty array (fetching some data from database). As a temporary fix, I'm checking if the array is empty and transform it in an array with an empty object. In that case, I just want a CSV with the headers.

knownasilya commented 6 years ago

Give v4.0.3 a try.

joaonice commented 6 years ago

@knownasilya thank you but I'm getting the same error because the first element of data is not an object.

if (!this.opts.fields && processedData.length === 0 || typeof processedData[0] !== 'object')
knownasilya commented 6 years ago

Ah, yes I'll fix that.

knownasilya commented 6 years ago

Try v4.0.4

joaonice commented 6 years ago

Thank you!

shadabk92 commented 4 years ago

Try v4.0.4

not working, still getting the same error image

knownasilya commented 4 years ago

Try v4.5.4

shadabk92 commented 4 years ago

with 4.5.4 and 4.0.4 both versions

  // CSV Stuff
  public csvFileName = 'taluka.csv';
  public fieldsData = ['id','name'];

  getCSVDownloadLink() {

    return this.generateCSVDownloadLink({
      filename: this.csvFileName,
      data: this.json2csv_data,
      fields: ['id','name'],
      includeEmptyRows: true
    });
  }

  public generateCSVDownloadLink(options: { filename: string, data: any[], fields: string[], includeEmptyRows?: boolean}): SafeUrl {
    const opts = {output: [options.filename, ['id','name'], options.includeEmptyRows]};
    const csv = json2csv.parse(options.data, opts);

    return this.domSanitizer.bypassSecurityTrustUrl('data:text/csv,' + encodeURIComponent(csv));
  }

We are using it in this fashion, on load t works as options.includeEmptyRows is present but fails when button click is initiated with the same error on each time the button is clicked.

Is our fields field wrongly used here ? @knownasilya

juanjoDiaz commented 4 years ago
const opts = {output: [options.filename, ['id','name'], options.includeEmptyRows]};

seems wrong to me.

I'd have expected:

const opts = {
  output: options.filename,
  options.fields,
  options.includeEmptyRows
};
knownasilya commented 4 years ago

@juanjoDiaz thanks for picking this up, been crazy over here with a move and a 2 month old 😆

vishnewsuraj commented 2 years ago

I am getting this error in node JS

knownasilya commented 2 years ago

Make sure your options are correct, like @juanjoDiaz mentioned in https://github.com/zemirco/json2csv/issues/280#issuecomment-573155448

MehediHasan0009 commented 2 years ago

Try v4.5.4

I installed 5.0.7 version. But getting this error. Should I move to v4.5.4?

MehediHasan0009 commented 2 years ago

Try v4.5.4

I installed 5.0.7 version. But getting this error. Should I move to v4.5.4?

I've moved to that version but still getting the error. Any suggestion?

knownasilya commented 2 years ago

@MehediHasan0009 did you try the https://www.npmjs.com/package/json2csv/v/6.0.0-alpha.1 release?

ShahSheel commented 1 year ago

We have the issue also in the latest release.. has this not been fixed yet?

juanjoDiaz commented 1 year ago

This repo was abandoned. You should move to https://github.com/juanjoDiaz/json2csv which is the continuation of this project. There you can find the latests versions where this was fixed long time ago.