Skip to content

axwaxw/json2csv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

json2csv

What Does This Do?

This is a package for Meteor

json2csv allows you to generate a csv file from an array or a Meteor collection. This is useful for quick-and-dirty import/export or for spreading data in excel.

Use

json2csv takes three arguments: json2csv(array, headings, quotes)

array - [array] or [function]

This could be any array, or a function that returns an array, such as Meteor's collection.find()fetch() function

// a simple array
[
  {firstname: "Alex", lastname: "Webster"},
  {firstname: "Jeff", lastname: "Wode"}
]

// a Meteor collection
Names.find().fetch()

// part of a Meteor collection
Names.find({firstname: "Alex"}, {sort: {lastname: -1}}).fetch()

headings - [boolean]

Whether or not to include a header line of field names in the csv

quotes - [boolean]

Whether or not to wrap csv values in double quotation marks ""

References

See http://docs.meteor.com/#find and http://docs.meteor.com/#fetch for details on Meteor's collection.find().fetch() functions

Examples

  // simply covert an array
  var array = [
    {firstname: "Alex", lastname: "Webster"}
    {firstname: "Jeff", lastname: "Wode"}
  ]
  var csv = json2csv(array, true, false)
  
  // or return an entire Meteor collection
  var csv = json2csv(Names.find().fetch(), true, true)
  
  // or return part of a Meteor collection
  var csv = json2csv(Names.find({firstname: "Alex"}, {sort: {lastname: -1}}).fetch(), true, true)
  

You could use this to add a 'download' button to a template as follows

HTML (using Bootstrap)

  <a href="#" class="btn btn-default" role="button" id="download">Download</a>

javaScript

  Template.orders.events({
    'click #download': function (e) {       
      csv = json2csv(Orders.find().fetch(), true, true)     
      e.target.href = "data:text/csv;charset=utf-8," + escape(csv) 
      e.target.download = "orders.csv";    
    }
  });

Limitations

This is only intended for 'flat' files so you should filter collections accordingly.

Credits

json2csv is based substantially on Joseph Sturtevant's fiddle, which was in turn based on Zachary's stackoverflow answer

Install

With Meteorite installed:

$ mrt add json2csv

About

Convert JSON arrays or Meteor collections to csv

Resources

Stars

Watchers

Forks

Packages

No packages published