New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

json-csv

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-csv

Export a richly structured, JSON array to CSV

  • 0.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.4K
decreased by-52.98%
Maintainers
1
Weekly downloads
 
Created
Source

json-csv

Simple CSV export module that can export a rich JSON array of objects to CSV.

Usage

var csv = require('json-csv')
csv.toCSV(args, callback)

var callback = function(err,csv) {
  //csv contains string of converted data in CSV format.
}

Streaming

var csv = require('json-csv')
var reader = csv.createReadStream(args)
reader.on('end', function() {
  //all done
})
reader.pipe(something_writable)

Arguments:

  {
    //required: array of data
    data : [],

    //field definitions for CSV export
    fields :
    [
      {
        //required: field name for source value
        name : 'string',

        //required: column label for CSV header
        label : 'string',

        //optional: filter to tranfsorm value before exporting
        filter : function(value) { return value; }
      }
    ]
  }

Example

Simple structure with basic CSV conversion.

var csv = require('../json-csv')
var items = [
  {
    name : 'fred',
    email : 'fred@somewhere',
    amount : 1.02
  },
  {
    name : 'jo',
    email : 'jo@somewhere',
    amount : 1.02
  },
  {
    name : 'jo with a comma,',
    email : 'jo@somewhere',
    amount : 1.02
  },
  {
    name : 'jo with a quote"',
    email : 'jo@somewhere',
    amount : 1.02
  }]

csv.toCSV({
  data : items,
  fields : [
    {
        name : 'name',
        label : 'Name',
        quoted : true
    },
    {
        name : 'email',
        label : 'Email'
    },
    {
        name : 'amount',
        label : 'Amount'
    }
  ]},
  function(err,csv) {
    console.log(csv);
});

//OR Streaming
csv.createReadStream({
  data : items,
  fields : [
    {
        name : 'name',
        label : 'Name',
        quoted : true
    },
    {
        name : 'email',
        label : 'Email'
    },
    {
        name : 'amount',
        label : 'Amount'
    }
  ]}).pipe(process.stdout);

Generates Output:

Name,Email,Amount
"fred",fred@somewhere,1.02
"jo",jo@somewhere,1.02
"jo with a comma,",jo@somewhere,1.02
"jo with a quote""",jo@somewhere,1.02

Here's a little more advanced sample that uses sub-structures and a filter for manipulating output for individual columns.

var csv = require('json-csv')
var items = [
  {
    contact : {
      company : 'Widgets, LLC',
      name : 'John Doe',
      email : 'john@widgets.somewhere'
    },
    registration : {
      year : 2013,
      level : 3
    }
  },
  {
    contact : {
      company : 'Sprockets, LLC',
      name : 'Jane Doe',
      email : 'jane@sprockets.somewhere'
    },
    registration : {
      year : 2013,
      level : 2
    }
  }
];

csv.toCSV({
  data : items,
  fields : [
    {
      name : 'contact.company',
      label : 'Company'
    },
    {
      name : 'contact.name',
      label : 'Name'
    },
    {
      name : 'contact.email',
      label : 'Email'
    },
    {
      name : 'registration.year',
      label : 'Year'
    },
    {
      name : 'registration.level',
      label : 'Level',
      filter : function(value) {
        switch(value) {
          case 1 : return 'Test 1'
          case 2 : return 'Test 2'
          default : return 'Unknown'
        }
      }
    }]
  },
  function(err,csv) {
    console.log(csv);
  });

Generates Output:

Company,Name,Email,Year,Level
"Widgets, LLC",John Doe,john@widgets.somewhere,2013,Unknown
"Sprockets, LLC",Jane Doe,jane@sprockets.somewhere,2013,Test 2

Pipe to File (Using example above):

var fs = require("fs")
var out = fs.createWriteStream("output.csv", {encoding: 'utf8'})
var reader = csv.createReadStream({
  data : items,
  fields : [
    {
      name : 'contact.company',
      label : 'Company'
    },
    {
      name : 'contact.name',
      label : 'Name'
    },
    {
      name : 'contact.email',
      label : 'Email'
    },
    {
      name : 'registration.year',
      label : 'Year'
    },
    {
      name : 'registration.level',
      label : 'Level',
      filter : function(value) {
        switch(value) {
          case 1 : return 'Test 1'
          case 2 : return 'Test 2'
          default : return 'Unknown'
        }
      }
    }]
  })

reader.on('end', function() {
  console.log("done")
})
reader.pipe(out)

Keywords

FAQs

Package last updated on 23 Jul 2014

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc