This module makes easy to convert JSON to CSV and its very customizable.
- v1.4.2 - default date handler return date.toLocaleString (jclay)
- v1.3.2 - fix userOptions optional
- v1.3.1 - object & array test
- v1.3.0 - some bug fixes + mocha tests/ + removed options handleArray and handleObject
- v1.2.2 - special chars escaping improvement (papswell)
- v1.2.0 - orderHeaders defaults to false
- v1.1.2 - Escaping text content if needed (papswell)
- v1.1.0 - Expose library as CLI (cburgmer)
- v1.0.7 - Fix for prototyped properties
- v1.0.6 - Create new lines in the CSV file to handle JSON objects with arrays
Installation command is npm install jsonexport
.
Run tests with npm test
.
var jsonexport = require('jsonexport');
jsonexport({lang: 'Node.js',module: 'jsonexport'}, {rowDelimiter: '|'}, function(err, csv){
if(err) return console.log(err);
console.log(csv);
});
var jsonexport = require('jsonexport');
var contacts = [{
name: 'Bob',
lastname: 'Smith'
},{
name: 'James',
lastname: 'David'
},{
name: 'Robert',
lastname: 'Miller'
},{
name: 'David',
lastname: 'Martin'
}];
jsonexport(contacts,function(err, csv){
if(err) return console.log(err);
console.log(csv);
});
name,lastname
Bob,Smith
James,David
Robert,Miller
David,Martin
var jsonexport = require('jsonexport');
var contacts = [{
name: 'Bob',
lastname: 'Smith',
family: {
name: 'Peter',
type: 'Father'
}
},{
name: 'James',
lastname: 'David',
family:{
name: 'Julie',
type: 'Mother'
}
},{
name: 'Robert',
lastname: 'Miller',
family: null,
location: [1231,3214,4214]
},{
name: 'David',
lastname: 'Martin',
nickname: 'dmartin'
}];
jsonexport(contacts,function(err, csv){
if(err) return console.log(err);
console.log(csv);
});
lastname,name,family.type,family.name,nickname,location
Smith,Bob,Father,Peter,,
David,James,Mother,Julie,,
Miller,Robert,,,,1231,3214,4214
Martin,David,,,dmartin,
var jsonexport = require('jsonexport');
var stats = {
cars: 12,
roads: 5,
traffic: 'slow'
};
jsonexport(stats,function(err, csv){
if(err) return console.log(err);
console.log(csv);
});
cars,12
roads,5
traffic,slow
var jsonexport = require('jsonexport');
var stats = {
cars: 12,
roads: 5,
traffic: 'slow',
speed: {
max: 123,
avg: 20,
min: 5
},
size: [10,20]
};
jsonexport(stats,function(err, csv){
if(err) return console.log(err);
console.log(csv);
});
cars,12
roads,5
traffic,slow
speed.max,123
speed.avg,20
speed.min,5
size,10;20
In order to get the most of out of this module, you can customize many parameters and functions.
####Options
headerPathString
-String
Used to create the propriety path, defaults to.
examplecontact: {name: 'example}
=contact.name
rowDelimiter
-String
Change the file row delimiter- Defaults to
,
(cvs format). - Use
\t
for xls format. - Use
;
for (windows excel .csv format).
- Defaults to
textDelimiter
-String
The character used to escape the text content if needed (default to"
)endOfLine
-String
Replace the OS default EOL.mainPathItem
-String
Every header will have themainPathItem
as the base.arrayPathString
-String
This is used to output primitive arrays in a single column, defaults to;
booleanTrueString
-String
Will be used instead oftrue
.booleanFalseString
-String
Will be used instead offalse
.includeHeaders
-Boolean
Set this option to false to hide the CSV headers.orderHeaders
-Boolean
The most used columns are shown first. (defaults tofalse
).undefinedString
-String
If you want to display a custom value for undefined strings, use this option. Defaults toverticalOutput
-Boolean
Set this option to false to create a horizontal output for JSON Objects, headers in the first row, values in the second.handleString
-Function
Use this to customize allStrings
in the CSV file.handleNumber
-Function
Use this to customize allNumbers
in the CSV file.handleBoolean
-Function
Use this to customize allBooleans
in the CSV file.handleDate
-Function
Use this to customize allDates
in the CSV file. (default to date.toLocaleString)
Lets say you want to prepend a text to every string in your CSV file, how to do it?
var jsonexport = require('jsonexport');
var options = {
handleString: function(string, name){
return 'Hey - ' + string;
}
};
jsonexport({lang: 'Node.js',module: 'jsonexport'}, options, function(err, csv){
if(err) return console.log(err);
console.log(csv);
});
The output would be:
lang,Hey - Node.js
module,Hey - jsonexport