kaue / jsonexport

{} → :page_facing_up: it's easy to convert JSON to CSV
http://kaue.github.io/jsonexport/
Apache License 2.0
247 stars 41 forks source link

Column values with zero (0) are being replaced with "" #76

Closed gannons closed 4 years ago

gannons commented 4 years ago

Noticed that on upgrading from 2.4.1 to 2.5.2 (and also 3.0.0) that the library has started to convert columns with integer values of 0 into "".

From a quick look through the code I think this happened due to

const fillAndPush = (row) => rows.push(row.map(col => col || ''));

https://github.com/kaue/jsonexport/blob/master/lib/parser/csv.js#L89

committed in

https://github.com/kaue/jsonexport/commit/40987acafe5887386e0827680326392d0bb8863f

kaue commented 4 years ago

Indeed looks like the problem is because we are doing col || '' at https://github.com/kaue/jsonexport/blob/master/lib/parser/csv.js#L89

Thats an pretty easy fix, i would replace that with something like col == null ? '' : col Feel free to open a pull request for this fix if you have time. Otherwise i will prob work on this later tonight.

We should probably include a test to make sure this does not happen in the future as well.

@gannons thanks for taking the time to report and also for pointing out where the issue is :)

kaue commented 4 years ago

should be fixed in jsonexport v3.0.1

g-ongenae commented 4 years ago

Still having this issue. (ts-node: v9.0.0, nodejs: 14.13.1, jsonexport: v3.0.1) But only on object not on array, for instance:

Input:

const value = {
  name: "Bob",
  lastName: "Smith",
  age: 0,
  family: {
    name: "Peter",
    type: "Father",
    size: 0,
  },
  location: [1231, 3214, 4214, 0],
};

jsonexport(value)
  .then(console.log)
  .catch(console.error);

Output:

name,Bob
lastName,Smith
age,
family.name,Peter
family.type,Father
family.size,
location,1231;3214;4214;0