json-dsv
Transform JSON to delimiter-separated values, such as CSV and TSV. Supports streams.
Installation
npm install json-dsv
Usage
Streaming
var JsonDSV = require('json-dsv');
var transformer = new JsonDSV(options);
readableStream
.pipe(transformer.dsv(addlOptions))
.pipe(writableStream);
Buffered
var JsonDSV = require('json-dsv');
var transformer = new JsonDSV(options);
var data = data;
transformer.dsv(data, options, function(err, dsv) {
});
Options / Defaults
{
delimiter: ',',
default: ''
includeHeader: true,
newLine: '\r\n',
fields: [
{
label: 'some label',
value: 'path.to.something',
default: 'NULL'
},
{
label: 'some label',
value: function(row) {
return row.path1 + row.path2;
},
default: 'NULL'
},
{
value: ['path', 'to.something']
},
'simplepath'
'path.to.value'
]
}
API
# new JsonDSV(options)
Constructs a new JSON-DSV transformer.
# JsonDSV.dsv([addlOptions])
Transforms data
to DSV (CSV by default). Streams data per line.
.csv
and .tsv
are available as convenience methods.
Specified addlOptions
override options
.
var options = {
fields: [{value: 'make', label: 'Brand'}, 'model']
};
var data = [
{ make: 'nissan', model: '350z'},
{ make: 'bmw', model: '328i'}
];
var JsonDSV = require('json-dsv');
var transformer = new JsonDSV(options);
var es = require('event-stream');
es.readArray(data)
.pipe(transformer.dsv())
.pipe(process.stdout);
# JsonDSV.dsv(data[, addlOptions], callback])
Transforms data
to DSV (CSV by default). Callback passes on buffered output.
.csv
and .tsv
are available as convenience methods.
Specified addlOptions
override options
.
var options = {
fields: [{value: 'make', label: 'Brand'}, 'model']
};
var data = [
{ make: 'nissan', model: '350z'},
{ make: 'bmw', model: '328i'}
];
var JsonDSV = require('json-dsv');
var transformer = new JsonDSV(options);
transformer.dsv(data, function(err, csv) {
console.log(csv);
});
Tests
npm test