Created
June 2, 2022 11:20
-
-
Save jpalala/e4a721e7a7cec3dea5678b2917934043 to your computer and use it in GitHub Desktop.
write a csv stream with json2csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { createReadStream, createWriteStream } = require('fs'); | |
const { AsyncParser } = require('json2csv'); | |
const fields = ['field1', 'field2', 'field3']; | |
const opts = { fields }; | |
const transformOpts = { highWaterMark: 8192 }; | |
// Using the promise API | |
const input = createReadStream(inputPath, { encoding: 'utf8' }); | |
const asyncParser = new JSON2CSVAsyncParser(opts, transformOpts); | |
const parsingProcessor = asyncParser.fromInput(input); | |
parsingProcessor.promise() | |
.then(csv => console.log(csv)) | |
.catch(err => console.error(err)); | |
// Using the promise API just to know when the process finnish | |
// but not actually load the CSV in memory | |
const input = createReadStream(inputPath, { encoding: 'utf8' }); | |
const output = createWriteStream(outputPath, { encoding: 'utf8' }); | |
const asyncParser = new JSON2CSVAsyncParser(opts, transformOpts); | |
const parsingProcessor = asyncParser.fromInput(input).toOutput(output); | |
parsingProcessor.promise(false).catch(err => console.error(err)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment