zemirco / json2csv

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

fix: Fix issue with unwind and empty arrays creating an extra column #496

Closed juanjoDiaz closed 4 years ago

juanjoDiaz commented 4 years ago

Fixes #494 in v5

coveralls commented 4 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling e3d845d893777d21e1dc42f945118cbd7be0f65d on juanjoDiaz:fix_unwind_bug_v5 into b37e242467b8d83bc21476e6e57fbf9c37962f35 on zemirco:v5.

martinfrancois commented 4 years ago

I actually tested this in our codebase and can confirm that it fixes #494, thanks a lot for the great work @juanjoDiaz !

knownasilya commented 4 years ago

Released as v5.0.4

martinfrancois commented 4 years ago

Thanks a lot @juanjoDiaz and @knownasilya you are awesome! 👍

martinfrancois commented 4 years ago

@juanjoDiaz I'm not sure if this is intended behavior or not, I just noticed the new introduced code modifies the objects directly, we are using TypeScript and our data is marked as readonly, which is why we noticed this part of the code:

    if (pathArray.length === 1) {
      delete obj[key];
      return obj;
    }

The operation delete obj[key]; seems to delete this on the original object as well. You can reproduce this by making a copy of the data that you pass into parse in a test before running parse, then after running parse, compare the copy with the object that was passed in. Then you will see that the empty array has been removed from the original data as well in a case like it was described in the issue.

juanjoDiaz commented 4 years ago

Dammit! Sometimes too fast is not too good...

This highlights that we should really move to typescript and avoid this type of thing...