mrodrig / json-2-csv

Convert JSON to CSV *or* CSV to JSON!
https://mrodrig.github.io/json-2-csv
MIT License
415 stars 57 forks source link

Incorrect parsing of wrapped values when the new line is right after the starting quote #240

Closed symbianm closed 10 months ago

symbianm commented 11 months ago

Background Information

The issue I'm reporting is with:

I have...

Expected Behavior

Quoted values split across multiple lines should be correctly parsed.

Actual Behavior

The parser behaves incorrectly when the new line is right after the starting quote.

Data Sample

CSV:

"User","Rental","City","State","ZipCode","Lead","Created","Qualified","Notes","Status","Details","Rating","Feedback","External Id"
"Leasing  Team","xxxxx North Sabre Drive","San Tan Valley","AZ","85142","AaronMiriam McAlpine","10/1/2023 5:25 PM MST","10/1/2023 5:27 PM MST","","Confirmed","10/1/2023 5:30 PM MST","5","
Layout was nice. Landscape needed work. But nice otherwise.","a001K00000vxWbxxxx"

Code Example

const fs = require('fs');
const { csv2json } = require('json-2-csv');

(async () => {
  const contentString = fs.readFileSync('test.csv');
  const jsonData = await csv2json(contentString.toString(), {
    parseValue: (fieldValue) => fieldValue, // stop converting values
  });
  console.log(jsonData);

  // Result
  // [
  //   {
  //     User: 'Leasing  Team',
  //     Rental: '34517 North Sabre Drive',
  //     City: 'San Tan Valley',
  //     State: 'AZ',
  //     ZipCode: '85142',
  //     Lead: 'AaronMiriam McAlpine',
  //     Created: '10/1/2023 5:25 PM USMST',
  //     Qualified: '10/1/2023 5:27 PM USMST',
  //     Notes: '',
  //     Status: 'Confirmed',
  //     Details: '10/1/2023 5:30 PM USMST',
  //     Rating: '5',
  //     Feedback: '',
  //     'External Id': undefined
  //   },
  //   {
  //     User: 'Layout was nice. Landscape needed work. But nice otherwise."',
  //     Rental: 'a001K00000vxWbFQAU',
  //     City: undefined,
  //     State: undefined,
  //     ZipCode: undefined,
  //     Lead: undefined,
  //     Created: undefined,
  //     Qualified: undefined,
  //     Notes: undefined,
  //     Status: undefined,
  //     Details: undefined,
  //     Rating: undefined,
  //     Feedback: undefined,
  //     'External Id': undefined
  //   }
  // ]
})();

test.csv

Screenshot 2023-10-08 at 9 59 27
mrodrig commented 10 months ago

Thanks for reporting this @symbianm! I can confirm that this was a bug that was present in the CSV parsing logic when an EOL delimiter (typically newline) appeared inside a field (typically quotes) delimited value. This issue is resolved now in version 5.0.1. Please let me know if you run into any other issues.