zemirco / json2csv

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

Error in unwinding empty array #544

Closed documanish closed 2 years ago

documanish commented 3 years ago

Unwinding an empty array is emitting empty csv. Below is a sample code to reproduce the issue

const { Parser, transforms: { unwind } } = require('json2csv');
const myCars = [
  {
    "carModel": "Audi",
    "price": 0,
    "segments":[{
        "colors": ["blue"],
        "empty": []
        }
    ]
      }
];
const fields = ['carModel', 'price','segments', 'segments.colors', 'segments.empty'];
const transforms = [unwind({ paths: ['segments', 'segments.colors', 'segments.empty'], blankOut: true })];
const json2csvParser = new Parser({ fields, transforms });
const csv = json2csvParser.parse(myCars);
console.log(csv);

Output: "\"carModel\",\"price\",\"segments\",\"segments.colors\",\"segments.empty\""

juanjoDiaz commented 2 years ago

This look like a bug indeed. I'll look into it!

AndriiNyzhnyk commented 2 years ago

Hello @zemirco and @juanjoDiaz! I have spotted the same issue with the latest stable version 5.0.6. Also, I have noted that some similar issue(#497) was opened and even fixed(3b74735) a long time ago(in 2020). The current issue is not labeled to any fix or something like that.

What is the current status of it? Are you going to fix this in version 6 or backport this to the current? I can dive into it and try to fix it, but I need to know your expectations.

mh04ng commented 2 years ago

Hi, i have the same issue and it seems that it has a fix however whenever i tried to npm install -g json2csv, the files were not updated. I am not sure how to update the files.

knownasilya commented 2 years ago

Thanks for pointing this out, looks like we need to do a new release. Will try to do it this week