0

In Node.js, using fs.createReadStream() to stream a .csv file into PapaParse (a parser to convert csv into json), I would like to get the following:

INPUT

A;B;C
;;
1;2;3
;;
1;2;3

OUTPUT (empty lines are kept)

{ 'A': '', B: '', C: '' },
{ 'A': '1', B: '2', C: '3' },
{ 'A': '', B: '', C: '' },
{ 'A': '1', B: '2', C: '3' },

I've tried reading through the fs.createReadStream() and PapaParse documentation but couldn't find out a way to do this.

I'm using Excel to create the .csv files. In this case saving the file as .csv with utf8 enconding a last empty line is always present.

This is how I'm streaming the file

csvStream.on('data', async chunk => {
  /** conversão csv -> json */
  streamToJSON(String(chunk), ({ data: parsed }) => console.log(parsed));
}

CSV Input

A;B;C
;;
1;2;3
;;
1;2;3

Log output (An empty line with only one property is generated at the end)

{ 'A': '', B: '', C: '' },
{ 'A': '1', B: '2', C: '3' },
{ 'A': '', B: '', C: '' },
{ 'A': '1', B: '2', C: '3' },
{ 'A': '' }

Expected output

{ 'A': '', B: '', C: '' },
{ 'A': '1', B: '2', C: '3' },
{ 'A': '', B: '', C: '' },
{ 'A': '1', B: '2', C: '3' },

I would like to get rid of that last object in the stream process at best or during parsing, didn't want to deal with later in the code.

Luis Felipe
  • 13
  • 1
  • 4

0 Answers0