npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-json2csv

v0.3.6

Published

Express middleware to convert JSON to a CSV file

Downloads

60

Readme

express-json2csv

PLEASE NOTE THAT THIS IS A WORK IN PROGRESS MODULE. EXPECT SYNTAX CHANGES FREQUENTLY WHILE I WORK THROUGH EDGE CASES AND REAL WORLD USES.

This is currently being tested in an application that has to create 500,000+ row csv files.

Installation

$ npm install express-json2csv

Adding to Express

var express = require('express');
var json2csv = require('express-json2csv');

var app = express();

app.use(json2csv);

Syntax

res.csv(filename, data, columns, [options]);

Filename

This can be any string you wish. The .csv extension is automatically appended to the filename so it is not required.

Data

Type: Array

Data is a required argument. The columns prop property will reference the keys in each of the data objects you pass in to define the order.

Columns

Type: Array

Columns is a required argument and will define the order / rendering of your CSV file. You must provide at least the label and prop attribute for each column. There is also a optional render attribute you can pass if you want to mutate your data before rendering it to the CSV.

var columns = [{
  prop: 'column1',
  label: 'First Column',
  render: function(data, row) {
    return data + ' rendered content!';
  }
}, {
  prop: 'column2',
  label: 'Second Column'
}];

Options

Option | Type | Default | Description ------ | ---- | ------- | ----------- includeHeader | Boolean | true | Include header columns includeFooter | Boolean | false | Include footer columns

Examples

var express = require('express');
var json2csv = require('express-json2csv');

var app = express();

app.use(json2csv);

// mimicking a mongodb esque dataset
var data = [{
    _id: '5422d576df3d571846f7927a',
    name: 'John Doe',
    occupation: 'Doctor',
    age: '28',
    status: 'Single'
  }, {
    _id: '5422d576df3d571846f7927b',
    name: 'Mary Poppins',
    occupation: 'Nurse',
    age: '32',
    status: 'Divorced'
}];

// define the order of the columns and their labels here
var columns = [{
  prop: 'name',
  label: 'Name',
  render: function(data, row) {
    return data + ', US citizen';
  }
}, {
  prop: 'occupation',
  label: 'Occupation'
}, {
  prop: 'age',
  label: 'Age'
}, {
  prop: 'status',
  label: 'Current Status'
}]

app.get('/', function(req, res) {
  res.csv('filename', data, columns);
});

Todo

  • Finish tests

License

MIT