adaltas / adaltas/node-csv

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

Open
#326 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
4.3k
Forks
299
Avg merge
16h 19m
Merged PRs (30d)
1

Description

Describe the bug

When using the latest csv-stringify@6.0.5 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!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the reproduction with csv-stringify, Node.js write streams, and the nested object and array values shown in the issue. Trace how those values are serialized and quoted, then verify the output against the expected CSV examples while preserving correct handling of ordinary fields.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
data-engineering
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.