juanjoDiaz / json2csv

Flexible conversion between JSON and CSV
https://juanjodiaz.github.io/json2csv/
MIT License
308 stars 32 forks source link

where does json2csv come from? #13

Closed ashley-jisc closed 1 year ago

ashley-jisc commented 1 year ago

When looking at the examples, there are references to a json2csv but it's not imported in the code examples.

For instance the Node Transform example:

import { createReadStream, createWriteStream } from 'fs';
import { Transform } from '@json2csv/node';

const input = createReadStream(inputPath, { encoding: 'utf8' });
const output = createWriteStream(outputPath, { encoding: 'utf8' });
const parser = new Transform();

const processor = input.pipe(parser).pipe(output);

// You can also listen for events on the conversion and see how the header or the lines are coming out.
json2csv
  .on('header', (header) => console.log(header))
  .on('line', (line) => console.log(line));

Where does json2csv come from? There only appears to be a AsyncParser and Transform exported from @json2csv/node.

juanjoDiaz commented 1 year ago

Hi @ashley-jisc ,

The example is wrong. I will fix it.

You should use the listeners on the parser:

import { createReadStream, createWriteStream } from 'fs';
import { Transform } from '@json2csv/node';

const input = createReadStream(inputPath, { encoding: 'utf8' });
const output = createWriteStream(outputPath, { encoding: 'utf8' });
const parser = new Transform();

const processor = input.pipe(parser).pipe(output);

// You can also listen for events on the conversion and see how the header or the lines are coming out.
parser
  .on('header', (header) => console.log(header))
  .on('line', (line) => console.log(line))