serialize-stream

Stream as json, ndjson, or csv.


License
MIT
Install
npm install serialize-stream@1.1.0

Documentation

serialize-stream

Stream json, ndjson, or csv.

This is useful if you have an API and want to stream different formats depending on a param.

Uses csv-write-stream and JSONStream under the hood.

Example

var ss = require('serialize-stream')

ss('csv')
  .on('data', console.log)
  .end({a: 1, b: 2, c: 3})

// a,b,c
// 1,2,3

ss('json')
  .on('data', console.log)
  .end({a: 1, b: 2, c: 3})

// [
// {"a":1,"b":2,"c":3}
// ]

ss('ndjson')
  .on('data', console.log)
  .end({a: 1, b: 2, c: 3})

// {"a":1,"b":2,"c":3}

API

ss(format, [opts])

  • format is one of: 'csv', 'json', or 'ndjson'.

  • opts defaults:

{
  compact: false, // csv: will remove undefined keys (like in json)
  flatten: false // csv: will flatten nested objects using `object2dot`[1]
}

[1] object2dot

License

MIT