object-to-csv
A lightweight, robust & dependency-less Node.js module to convert JSON objects to CSV format.
• Installation
Latest version: 1.0.5
npm install object-to-csv
• Quick Start Example
const ObjectToCSV = require('object-to-csv');
let data = [
{
'make': 'Ford',
'model': 'Mustang',
'new': true
},
];
let otc = new ObjectToCSV({ data });
let csv = otc.getCSV();
• Tests
npm test
• Usage
Using the ObjectToCSV
module is easy.
The setter methods return this
which enables chaining functions. Warnings are presented when values are given which may cause issues with the module. Find everything else you need to know below.
• Constructor
The constructor of the ObjectToCSV
module allows you to set all of the options
up front in key
/value
format.
Usage Example:
let otc = new ObjectToCSV({
'keys': [],
'data': [],
'fileName': 'test-file-name',
'shouldExpandObjects': true,
'endOfLineValue': '\n',
'delimiter': ',',
'quote': '"',
'booleanValues': {'true':'Yes', 'false':'No'},
});
• Options
• keys
Keys for first row of csv. Set the keys in an array of objects according to the format below. Using the key
/as
format allows for you to set the key
in the data
object and display the as
within the final csv as a proper title for example.
If empty, the keys in the first object of the array will be used as column names.
- Type:
Array
- Default:
[]
- Format:
[ {key: '', as: ''}, ... ]
Usage Example:
let keys = [
{
key: 'make',
as: 'Make',
}
];
let otc = new ObjectToCSV({ keys });
keys = otc.keys;
• data
The data that fills the csv
- Type:
Array
- Default:
[]
- Format:
[ {...}, ... ]
Usage Example:
let data = [
{
'make': 'Ford',
'model': 'Mustang',
'new': true
},
];
let otc = new ObjectToCSV({ data });
data = otc.data;
• fileName
The file name for downloading the csv.
- Type:
String
- Default:
file
Usage Example:
let fileName = 'file-name';
let otc = new ObjectToCSV({ fileName });
fileName = otc.fileName;
• shouldExpandObjects
Should flatten nested objects. This is a boolean to check if JSON.stringify()
should be used on arrays and objects within the data
.
- Type:
Boolean
- Default:
false
Usage Example:
let shouldExpandObjects = false;
let otc = new ObjectToCSV({ shouldExpandObjects });
shouldExpandObjects = otc.shouldExpandObjects;
• endOfLineValue
Specify an end of line value for separating rows other than the default line ending.
Usage Example:
let endOfLineValue = '\n';
let otc = new ObjectToCSV({ endOfLineValue });
endOfLineValue = otc.endOfLineValue;
• delimiter
CSV delimiter of columns
Usage Example:
let delimiter = ',';
let otc = new ObjectToCSV({ delimiter });
delimiter = otc.delimiter;
• quote
The quote around cell values and column names.
Usage Example:
let quote = '"';
let otc = new ObjectToCSV({ quote });
quote = otc.quote;
• booleanValues
The values to show if a boolean value is present in the data for the csv.
- Type:
Object
- Default:
{ 'true': 'TRUE', 'false': 'FALSE' }
- Format:
{ 'true': 'Yes', 'false': 'No' }
Usage Example:
let booleanValues = { 'true': 'Yes', 'false': 'No' };
let otc = new ObjectToCSV({ booleanValues });
booleanValues = otc.booleanValues;
• Commands
• getCSV()
Get CSV as a string from given attributes
Usage Example:
let otc = new ObjectToCSV();
let csv = otc.getCSV();
Get response headers for sending a CSV download to the client
- Returns:
Object
- Format:
{'Content-Disposition': '...', 'Content-Type': '...'}
Usage Example:
let otc = new ObjectToCSV();
let responseHeaders = otc.getResponseHeaders();
let csv = otc.getCSV();
const http = require('http');
http.createServer((req, res) => {
const url = req.url;
if (url == '/download') {
res.setHeader('Content-Type', responseHeaders['Content-Type']);
res.setHeader('Content-Disposition', responseHeaders['Content-Disposition']);
res.write(csv, 'text/csv');
res.end();
}
}).listen(8080);
• Say Hi
Tweet at me: @markmiscavage.
• License
MIT License
Copyright (c) 2020 Mark Miscavage
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.