# express-json2csv

> Express middleware to convert JSON to a CSV file

Latest version **0.3.6** (published 2014-10-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-json2csv
pnpm add express-json2csv
yarn add express-json2csv
bun add express-json2csv
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.6 |
| Published | 2014-10-10 |
| First published | 2014-09-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 (+4 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Brandon Pierce |
| Maintainers | brandonjpierce |

## Links

- npm: https://www.npmjs.com/package/express-json2csv
- Repository: https://github.com/brandonjpierce/express-json2csv
- Issues: https://github.com/brandonjpierce/express-json2csv/issues
- npm.io page: https://npm.io/package/express-json2csv

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^2.4.1
- [arraystream](https://npm.io/package/arraystream.md) ^0.0.5

## Recent versions

- 0.3.6 (latest) — 2014-10-10
- 0.3.5 — 2014-10-10
- 0.3.4 — 2014-10-10
- 0.3.3 — 2014-10-10
- 0.3.2 — 2014-10-10
- 0.3.1 — 2014-10-08
- 0.3.0 — 2014-10-08
- 0.2.4 — 2014-10-03
- 0.2.3 — 2014-10-03
- 0.2.2 — 2014-10-03
- 0.2.1 — 2014-10-01
- 0.2.0 — 2014-10-01
- 0.1.6 — 2014-09-30
- 0.1.4 — 2014-09-30
- 0.1.3 — 2014-09-30
- … 3 more at https://npm.io/package/express-json2csv/versions

## 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

```bash
$ npm install express-json2csv
```

## Adding to Express

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

var app = express();

app.use(json2csv);
```
## Syntax

```js
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
```js
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
```js
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.

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

```js
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](LICENSE)

---
_Source: https://npm.io/package/express-json2csv · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
