jsonexport
This module makes easy to convert JSON to CSV and its very customizable.
Table of Contents
Usage
Installation command is npm install jsonexport
.
Run tests with npm test
.
var jsonexport = ; ;
CLI
Global installation command is npm install -g jsonexport
.
Convert JSON to CSV using cat data.json | jsonexport
or jsonexport data.json
Usage: jsonexport <JSON filename> <CSV filename>
Browser
Use the code in the folder named dist to run jsonexport in the browser
Browser Import Examples
Webpack
var jsonexport =
Typescript
Stream
var jsonexport = ;var fs = ; var reader = fs;var writer = fs; reader;
JSON Array Example
Simple Array
Code
var jsonexport = ; var contacts = name: 'Bob' lastname: 'Smith' name: 'James' lastname: 'David' name: 'Robert' lastname: 'Miller' name: 'David' lastname: 'Martin'; ;
Result
name,lastname
Bob,Smith
James,David
Robert,Miller
David,Martin
Complex Array
Code
var 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: 123132144214 name: 'David' lastname: 'Martin' nickname: 'dmartin'; ;
Result
name,lastname,family.name,family.type,family,location,nickname
Bob,Smith,Peter,Father
James,David,Julie,Mother
Robert,Miller,,,,1231;3214;4214
David,Martin,,,,,dmartin
JSON Object Example
Simple Object
Code
var jsonexport = ; var stats = cars: 12 roads: 5 traffic: 'slow'; ;
Result
cars,12
roads,5
traffic,slow
Complex Object
Code
var jsonexport = ; var stats = cars: 12 roads: 5 traffic: 'slow' speed: max: 123 avg: 20 min: 5 size: 1020; ;
Result
cars,12
roads,5
traffic,slow
speed.max,123
speed.avg,20
speed.min,5
size,10;20
Options
In order to get the most of out of this module, you can customize many parameters and functions.
headerPathString
-String
Used to create the propriety path, defaults to.
examplecontact: {name: 'example}
=contact.name
fillGaps
-Boolean
Set this option if don't want to have empty cells in case of an object with multiple nested items (array prop), defaults tofalse
Issue #22headers
-Array
Used to set a custom header order, defaults to[]
example['lastname', 'name']
rename
-Array
Used to set a custom header text, defaults to[]
example['Last Name', 'Name']
mapHeaders
-Function
Post-process headers after they are calculated with delimiters, examplemapHeaders: (header) => header.replace(/foo\./, '')
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"
)forceTextDelimiter
-Boolean
Set this option to true to wrap every data item and header in the textDelimiter. Defaults tofalse
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.undefinedString
-String
If you want to display a custom value for undefined strings, use this option. Defaults to.
verticalOutput
-Boolean
Set this option to false to create a horizontal output for JSON Objects, headers in the first row, values in the second.typeHandlers
-{typeName:(value, index, parent)=>any
A key map of constructors used to match by instance to create a value using the defined function (see example)
Deprecated Options (Use typeHandlers)
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)
typeHandlers
Define types by constructors and what function to run when that type is matched
var jsonexport = ; //datavar contacts = 'a' : Buffer 'b' : Buffer 'x' : 22 {return 'bad ace'}; var options= //definitions to type cast typeHandlers: { return 'replaced-array'; } { return 'replaced-boolean'; } { return } { return 'replaced-number'; } { return 'replaced-string'; } { return value } ;
The output would be:
a,a2b
b,other field
x,replaced-number
z,bad ace
When using typeHandlers, Do NOT do this
var options={
typeHandlers:{
Object:function(value,index,parent){
return 'EVERYTHING IS AN OBJECT';
}
}
}
It is NOT an error, however the recursive result becomes illegable functionality strings