
Security News
Meet Socket at Black Hat Europe and BSides London 2025
Socket is heading to London! Stop by our booth or schedule a meeting to see what we've been working on.
object-to-csv
Advanced tools
A lightweight, robust & dependency-less Node.js module to convert JSON objects to CSV format.
Latest version: 1.0.5
npm install object-to-csv
//1. Import the module
const ObjectToCSV = require('object-to-csv');
//2. Set data
let data = [
{
'make': 'Ford',
'model': 'Mustang',
'new': true
},
];
//3. Set up CSV
let otc = new ObjectToCSV({ data });
//4. Get CSV
let csv = otc.getCSV();
//Outputs:
// "Make","Model","Is The Car New"
// "Ford","Mustang","TRUE"
npm test
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.
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'},
});
keysKeys 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.
Array[][ {key: '', as: ''}, ... ]Usage Example:
let keys = [
{
key: 'make',
as: 'Make',
}
];
//Set keys in 3 different ways
let otc = new ObjectToCSV({ keys });
// otc.setKeys(keys);
// otc.keys = keys;
//Get
keys = otc.keys;
dataThe data that fills the csv
Array[][ {...}, ... ]Usage Example:
let data = [
{
'make': 'Ford',
'model': 'Mustang',
'new': true
},
];
//Set data in 3 different ways
let otc = new ObjectToCSV({ data });
// otc.setData(data);
// otc.data = data;
//Get
data = otc.data;
fileNameThe file name for downloading the csv.
StringfileUsage Example:
let fileName = 'file-name';
//Set file name in 3 different ways
let otc = new ObjectToCSV({ fileName });
// otc.setFileName(fileName);
// otc.fileName = fileName;
//Get
fileName = otc.fileName;
shouldExpandObjectsShould flatten nested objects. This is a boolean to check if JSON.stringify() should be used on arrays and objects within the data.
BooleanfalseUsage Example:
let shouldExpandObjects = false;
//Set shouldExpandObjects in 3 different ways
let otc = new ObjectToCSV({ shouldExpandObjects });
// otc.setShouldExpandObjects(shouldExpandObjects);
// otc.shouldExpandObjects = shouldExpandObjects;
//Get
shouldExpandObjects = otc.shouldExpandObjects;
endOfLineValueSpecify an end of line value for separating rows other than the default line ending.
String\nUsage Example:
let endOfLineValue = '\n';
//Set endOfLineValue in 3 different ways
let otc = new ObjectToCSV({ endOfLineValue });
// otc.setEndOfLineValue(endOfLineValue);
// otc.endOfLineValue = endOfLineValue;
//Get
endOfLineValue = otc.endOfLineValue;
delimiterCSV delimiter of columns
String,Usage Example:
let delimiter = ',';
//Set delimiter in 3 different ways
let otc = new ObjectToCSV({ delimiter });
// otc.setDelimiter(delimiter);
// otc.delimiter = delimiter;
//Get
delimiter = otc.delimiter;
quoteThe quote around cell values and column names.
String"Usage Example:
let quote = '"';
//Set quote in 3 different ways
let otc = new ObjectToCSV({ quote });
// otc.setQuote(quote);
// otc.quote = quote;
//Get
quote = otc.quote;
booleanValuesThe values to show if a boolean value is present in the data for the csv.
Object{ 'true': 'TRUE', 'false': 'FALSE' }{ 'true': 'Yes', 'false': 'No' }Usage Example:
let booleanValues = { 'true': 'Yes', 'false': 'No' };
//Set quote in 3 different ways
let otc = new ObjectToCSV({ booleanValues });
// otc.setBooleanValues(booleanValues);
// otc.booleanValues = booleanValues;
//Get
booleanValues = otc.booleanValues;
getCSV()Get CSV as a string from given attributes
StringUsage Example:
let otc = new ObjectToCSV();
let csv = otc.getCSV();
getResponseHeaders()Get response headers for sending a CSV download to the client
Object{'Content-Disposition': '...', 'Content-Type': '...'}Usage Example:
let otc = new ObjectToCSV();
let responseHeaders = otc.getResponseHeaders();
let csv = otc.getCSV();
//Example
//1. Set up your server
//In this case, we're just going to use the http module
const http = require('http');
//2. Create server
http.createServer((req, res) => {
const url = req.url;
//3. Set up a route on your sever to download a test csv
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);
Tweet at me: @markmiscavage.
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.
FAQs
Convert array of objects to a CSV. No dependencies.
The npm package object-to-csv receives a total of 455 weekly downloads. As such, object-to-csv popularity was classified as not popular.
We found that object-to-csv demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
Socket is heading to London! Stop by our booth or schedule a meeting to see what we've been working on.

Security News
OWASP’s 2025 Top 10 introduces Software Supply Chain Failures as a new category, reflecting rising concern over dependency and build system risks.

Research
/Security News
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.