node-csv icon indicating copy to clipboard operation
node-csv copied to clipboard

When streaming writes with nodejs, `csv-stringify` will return twice amount of quotations if the line contains nested json objects

Open byhow opened this issue 3 years ago • 3 comments

Describe the bug

When using the latest [email protected] or the @5.x versions, when I use write streams to create csv files out of objects, it will add twice amout of quotations as expected. e.g. "{""id"": ""random-hex"", ""count"": 1, ""user"":""userID""}"

To Reproduce The code to reproduce:

const fileStream = fs.createWriteStream(writeFile, { flags: 'wx' });
const jsonArray = [
  {
    id: { nested: 'random-hex' }, // nested object
    count: 1,
    user: 'userID',
  },
  {
    id: { nested: 'random-hex' },
    count: 1,
    user: ['userID'], // array
  },
];
stringify(jsonArray, {
  header: true,
  // cast: {
  //   object: function(val) {
  //     return JSON.stringify(val);  // tried using stringify but did not work out
  //   },
  // },
}, function(err, output) {
  if (err) {
    console.error(err);
  }
  fileStream.write(output);
}).on('finish', () => console.log(`Done writing to:  ${writeFile.yellow}`));

will give out:

id,count,user
"{""nested"":""random-hex""}",1,userID
"{""nested"":""random-hex""}",1,"[""userID""]"

Was wondering how to parse those special fields (I assume is something with cast) so that it is in the format of JSON. Expected to be:

id,count,user
{"nested":"random-hex"},1,userID
{"nested":"random-hex"},1,["userID"]

Thanks in advance!

byhow avatar Mar 02 '22 07:03 byhow

This is expected, the default quote is double quotation marks and the default escape is the same. You could change the quote option, with another special character. You could also disable it with false but your CSV will be broken if one of the fields contains a field delimiter character.

wdavidw avatar Mar 02 '22 18:03 wdavidw

Thanks for the pointer! @wdavidw I assume since this is an expected behavior, this is no longer an issue.

Would you happen to know in which way I can escape the json string so that it can produce the expected output (without double quotations marks) ? And yes false wouldn't work as it will break the json objects

byhow avatar Mar 05 '22 20:03 byhow

You must generate correct CSV content, in your case, look at the quote and escape options.

wdavidw avatar Mar 06 '22 19:03 wdavidw