What is jsonexport?
The jsonexport npm package is a utility that allows you to convert JSON data into CSV format. It is particularly useful for exporting data from JSON APIs or databases into a format that can be easily imported into spreadsheet applications like Microsoft Excel or Google Sheets.
What are jsonexport's main functionalities?
Basic JSON to CSV Conversion
This feature allows you to convert a simple JSON array into CSV format. The code sample demonstrates how to convert an array of JSON objects into a CSV string.
const jsonexport = require('jsonexport');
const data = [{"name":"John","age":30},{"name":"Jane","age":25}];
jsonexport(data, function(err, csv){
if(err) return console.error(err);
console.log(csv);
});
Nested JSON to CSV Conversion
This feature allows you to convert nested JSON objects into CSV format. The code sample demonstrates how to handle nested JSON structures and convert them into a flat CSV format.
const jsonexport = require('jsonexport');
const data = [{"name":"John","age":30,"address":{"city":"New York","state":"NY"}},{"name":"Jane","age":25,"address":{"city":"San Francisco","state":"CA"}}];
jsonexport(data, function(err, csv){
if(err) return console.error(err);
console.log(csv);
});
Custom Delimiters
This feature allows you to specify custom delimiters for the CSV output. The code sample demonstrates how to use a semicolon as the delimiter instead of the default comma.
const jsonexport = require('jsonexport');
const data = [{"name":"John","age":30},{"name":"Jane","age":25}];
const options = {delimiter: ';'};
jsonexport(data, options, function(err, csv){
if(err) return console.error(err);
console.log(csv);
});
Other packages similar to jsonexport
json2csv
The json2csv package is another popular library for converting JSON to CSV. It offers similar functionalities to jsonexport, including support for nested JSON objects and custom delimiters. However, json2csv provides more advanced features like field selection, field renaming, and data transformation.
csv-writer
The csv-writer package is a versatile library for writing CSV files. While it does not directly convert JSON to CSV, it allows you to write CSV files from JavaScript objects, making it a good alternative for more complex CSV writing needs. It offers features like custom headers, field transformations, and support for different CSV formats.
jsonexport
This module makes easy to convert JSON to CSV and its very customizable.
#Usage
Installation command is npm install jsonexport
.
var jsonexport = require('jsonexport');
jsonexport({lang: 'Node.js',module: 'jsonexport'}, {rowDelimiter: '|'}, function(err, csv){
if(err) return console.log(err);
console.log(csv);
});
##JSON Array Example
###Simple Array
####Code
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);
});
####Result
name;lastname
Bob;Smith
James;David
Robert;Miller
David;Martin
###Complex Array
####Code
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);
});
####Result
lastname;name;family.type;family.name;nickname;location
Smith;Bob;Father;Peter;;
David;James;Mother;Julie;;
Miller;Robert;;;;1231,3214,4214
Martin;David;;;dmartin;
##JSON Object Example
###Simple Object
####Code
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);
});
####Result
cars;12
roads;5
traffic;slow
###Complex Object
####Code
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);
});
####Result
cars;12
roads;5
traffic;slow
speed.max;123
speed.avg;20
speed.min;5
size;10,20
##Customization
In order to get the most of out of this module, you can customize many parameters, check the list:
Option : Default Value
headerPathString: '.',
rowDelimiter: ';',
endOfLine: null,
mainPathItem: null,
arrayPathString: ',',
booleanTrueString: null,
booleanFalseString: null,
includeHeaders: true,
orderHeaders: true,
undefinedString: '',
verticalOutput: true,
handleString: null,
handleNumber: null,
handleBoolean: null,
handleDate: null,
handleArray: null,
handleObject: null
###Handle Customization
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