zemirco / json2csv

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

Async parser does not transform array into comma-separated. #529

Closed kkorus closed 2 years ago

kkorus commented 3 years ago

My data in json:

[
 { name: "test name", tags: ["tags1", "tags2"] }
]

my code:

import { transforms, AsyncParser } from 'json2csv';

  protected getParser(): AsyncParser<DataCsvFields> {
    const fields = ['name', 'tags'];

    return new AsyncParser({
      fields,
      delimiter: ',',
      eol: '\n',
      transforms: [transforms.flatten({ arrays: true })],
    });
  }

    let data = await this.readData();
    let csv = '';
    const asyncParser = this.getParser();
    asyncParser.processor
      .on('data', chunk => {
        csv += chunk.toString();
      })
      .on('error', err => {});

    while (data.length) {
      asyncParser.input.push(JSON.stringify(data));
      data = await this.readData(data[data.length - 1].id);
    }
    asyncParser.input.push(null);

expected csv output:

name,tags
test name, "tags1, tags2"

current result:

name,tags
test name

So tags are missing, but when I remove transforms: [transforms.flatten({ arrays: true })], I will get:

name,tags
test name, "[""tag1"", ""tag2""]"

but I want to array value to be comma-separated.

juanjoDiaz commented 3 years ago

Hi @kkorus ,

The reason you are getting that output is because after using flatten, there is no tags field anymore. Try removing the fields option. The actual result is (notice that there is no tags column)

"name","tags.0","tags.1"
"test name","tags1","tags2"

For something close to your expected output, as you figured out, you don't use any transform and you get:

"name","tags"
"test name","[""tags1"",""tags2""]"

If you want to remove the brackets or inner quotes you need to use a custom selector. Or using v6 (only in pre-release now)

juanjoDiaz commented 2 years ago

Closing since help was provided and there was no answer.