Socket
Socket
Sign inDemoInstall

json-dsv

Package Overview
Dependencies
4
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    json-dsv

A transformer for JSON to delimiter-separated values, such as CSV and TSV


Version published
Maintainers
1
Install size
346 kB
Created

Readme

Source

json-dsv

Build status

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; // Object[]
transformer.dsv(data, options, function(err, dsv) {
  // buffered dsv result
});

Options / Defaults

{
  delimiter: ',', // use a different field separator char, eg `\t`
  default: '' // if value is undefined at `value` path
  includeHeader: true, // Boolean, determines whether or not CSV file will contain a title column
  newLine: '\r\n', // String, overrides the default OS line ending (i.e. `\n` on Unix and `\r\n` on Windows).
  fields: [
    // Supports label -> simple path
    {
      label: 'some label', // (optional, column will be labeled 'path.to.something' if not defined)
      value: 'path.to.something', // data.path.to.something
      default: 'NULL' // default if value is not found (optional, overrides `options.default` for column)
    },

    // Supports label -> derived value
    {
      label: 'some label', // Supports duplicate labels (required, else your column will be labeled [function])
      value: function(row) {
        return row.path1 + row.path2;
      },
      default: 'NULL' // default if value fn returns undefined
    },

    // Support pathname -> pathvalue
    'simplepath' // equivalent to {value:'simplepath'}
    'path.to.value' // also equivalent to {label:'path.to.value', value:'path.to.value'}
  ]
}

### API

<a name="JsonDSV" href="#JsonDSV">#</a> new <b>JsonDSV</b>(<i>options</i>)

Constructs a new JSON-DSV transformer.

<a name="JsonDSV_dsv_stream" href="#JsonDSV_dsv_stream">#</a> *JsonDSV*.<b>dsv</b>([<i>addlOptions</i>])

Transforms `data` to DSV (CSV by default). Streams data per line.

`.csv` and `.tsv` are available as convenience methods.

Specified `addlOptions` override `options`.

```js
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);

// Brand,model
// nissan,350z
// bmw,328i

# 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);
});

// Brand,model
// nissan,350z
// bmw,328i

Tests

npm test

Keywords

FAQs

Last updated on 03 Nov 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc