mk-json-2-csv
v3.0.2
Published
A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.
Downloads
5
Maintainers
Readme
json-2-csv
Convert JSON to CSV or CSV to JSON
This node module will convert an array of JSON documents to a CSV string. Column headings will be automatically generated based on the keys of the JSON documents. Nested documents will have a '.' appended between the keys.
It is also capable of converting CSV of the same form back into the original array of JSON documents. The columns headings will be used as the JSON document keys. All lines must have the same exact number of CSV values.
Installation
$ npm install json-2-csv
Usage
let converter = require('json-2-csv');
Upgrading?
Upgrading to v3 from v2? Check out the upgrade guide.
API
converter.json2csv(array, callback, options)
array
- An array of JSON documents to be converted to CSV.callback
- A function of the formfunction (err, csv)
;- This function will receive any errors and/or the string of CSV generated.
options
- (Optional) A JSON document specifying any of the following key value pairs:delimiter
- Document - Specifies the different types of delimitersfield
- String - Field Delimiter.- Default:
,
- Default:
wrap
- String - Wrap values in the delimiter of choice (e.g. wrap values in quotes).- Default:
"
- Default:
eol
- String - End of Line Delimiter.- Default:
\n
- Default:
excelBOM
- Boolean - Should a unicode character be prepended to allow Excel to open a UTF-8 encoded file with non-ASCII characters present.prependHeader
- Boolean - Should the auto-generated header be prepended as the first line in the CSV?- Default:
true
- Default:
sortHeader
- Boolean - Should the header keys be sorted in alphabetical order?- Default:
false
- Default:
trimHeaderFields
- Boolean - Should the header fields be trimmed?- Default:
false
- Default:
trimFieldValues
- Boolean - Should the field values be trimmed? (in development)- Default:
false
- Default:
checkSchemaDifferences
- Boolean - Should all documents have the same schema?- Default:
false
- Note: Change this to
false
if some documents are missing certain fields and you still want to convert the data.
- Default:
keys
- Array - Specify the keys (as strings) that should be converted.- Default:
null
- If you have a nested object (ie. {info : {name: 'Mike'}}), then set this to ['info.name']
- If you want all keys to be converted, then specify
null
or don't specify the option to utilize the default.
- Default:
For examples, please refer to the json2csv API Documentation (Link)
Promisified Version: converter.json2csvAsync(array, options)
Available in version 2.2.0
, this functionality makes use of promises from the bluebird
module.
converter.csv2json(csv, callback, options)
csv
- A string of CSVcallback
- A function of the formfunction (err, array)
; This function will receive any errors and/or the array of JSON documents generated.options
- (Optional) A JSON document specifying any of the following key value pairs:delimiter
- Document - Specifies the different types of delimitersfield
- String - Field Delimiter.- Default:
,
- Default:
wrap
- String - The character that field values are wrapped in.- Default:
"
- Default:
eol
- String - End of Line Delimiter.- Default:
\n
- Default:
excelBOM
- Boolean - Does the CSV contain a unicode character prepended in order to allow Excel to open a UTF-8 encoded file with non-ASCII characters present?- Default:
false
- Default:
trimHeaderFields
- Boolean - Should the header fields be trimmed?- Default:
false
- Default:
trimFieldValues
- Boolean - Should the field values be trimmed?- Default:
false
- Default:
keys
- Array - Specify the keys (as strings) that should be converted.- Default:
null
- If you have a nested object (ie.
{info : {name: 'Mike'}}
), then set this to['info.name']
- If you want all keys to be converted, then specify
null
or don't specify the option to utilize the default.
- Default:
For examples, please refer to the csv2json API Documentation (Link)
Promisified Version: csv2jsonAsync(csv, options)
Available in version 2.2.0
, this functionality makes use of promises from the bluebird
module.
Tests
$ npm test
Note: This requires mocha
, should
, and underscore
.
To see test coverage, please run:
$ npm run coverage
Current Coverage is:
Statements : 100% ( 258/258 )
Branches : 100% ( 124/124 )
Functions : 100% ( 49/49 )
Lines : 100% ( 256/256 )
Frequently Asked Questions (FAQ)
Please find the updated list (relocated to the Wiki) here: Frequently Asked Questions (Link)
Features
- Header Generation (per document keys)
- Allows for conversion of specific keys in both json2csv and csv2json via the options.keys parameter (as of 1.1.2)
- Verifies all documents have same schema (schema field order does not matter as of 1.1.0)
- Supports sub-documents natively
- Supports arrays as document values for both json2csv and csv2json
- Custom ordering of columns (see F.A.Q. for more information)
- Ability to re-generate the JSON documents that were used to generate the CSV (including nested documents)
- Allows for custom field delimiters, end of line delimiters, etc.
- Promisifiable via bluebird's .promisify() and .promisifyAll() (as of 1.1.1)
- Wrapped value support for json2csv and csv2json (as of 1.3.0)
- Support for multiple different schemas (as of 1.4.0)
- Promisified versions of the functions are now available by default: json2csvAsync, csv2jsonAsync (as of 2.2.0)
- RFC 4180 Compliance (as of 3.0.0)
- CLI functionality (as of 3.0.0)
csv2json test.csv -o output.json
- and
json2csv test.json -o output.csv -W -k arrayOfStrings -o output.csv