Node.js Transform a CSV Stream

Published on:

Here is all you need to transform a CSV stream.

#!/usr/bin/env node

'use strict';
const parser = require('csv-parse')({
  columns: true
});
const stringer = require('csv-stringify')({
  header: true
});
const transformer = new require('stream').Transform({
  transform(row, encoding, cb) {
    // row will be JSON with the CSV column names as property
    // names.
    // modify row however you like, or create a new object
    // to pass to the callback.
    cb(null, row);
  },
  objectMode: true
});
process.stdin
  .pipe(parser)
  .pipe(transformer)
  .pipe(stringer)
  .pipe(process.stdout);