json-2-csv
A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.
Last updated 17 days ago by mrodrig .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install json-2-csv 
SYNC missed versions from official npm registry.

json-2-csv

Convert JSON to CSV or CSV to JSON

Dependencies Downloads NPM version Minzipped Size Package Size Typings

Known Vulnerabilities Build Status Maintainability Test Coverage

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

CLI:

$ npm install @mrodrig/json-2-csv-cli

Upgrading?

Upgrading to v3 from v2? Check out the upgrade guide.

Usage

let converter = require('json-2-csv');

or

import { json2csv } from 'json-2-csv';

Looking for examples? Check out the Wiki: json-2-csv Wiki

API

converter.json2csv(array, callback, options)

  • array - An array of JSON documents to be converted to CSV.
  • callback - A function of the form function (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:
    • checkSchemaDifferences - Boolean - Should all documents have the same schema?
      • Default: false
      • Note: An error will be thrown if some documents have differing schemas when this is set to true.
    • delimiter - Document - Specifies the different types of delimiters
      • field - String - Field Delimiter.
        • Default: ,
      • wrap - String - Wrap values in the delimiter of choice (e.g. wrap values in quotes).
        • Default: "
      • eol - String - End of Line Delimiter.
        • Default: \n
    • emptyFieldValue - Any - Value that, if specified, will be substituted in for field values that are undefined, null, or an empty string.
      • Default: none
    • excelBOM - Boolean - Should a unicode character be prepended to allow Excel to open a UTF-8 encoded file with non-ASCII characters present.
    • expandArrayObjects - Boolean - Should objects in array values be deep-converted to CSV?
      • Default: false
      • Example:
      [
      	{ 
      		"specifications": [
      			{ "features": [...] },
      			{ "mileage": "5000" }
      		]
      	}
      ]
      
      • true uses the following keys:
        • ['specifications.features', 'specifications.mileage']
      • false uses the following keys:
        • ['specifications']
      • Note: This may result in CSV output that does not map back exactly to the original JSON. See #102 for more information.
    • 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.
    • prependHeader - Boolean - Should the auto-generated header be prepended as the first line in the CSV?
      • Default: true
    • sortHeader - Boolean - Should the header keys be sorted in alphabetical order?
      • Default: false
    • trimFieldValues - Boolean - Should the field values be trimmed?
      • Default: false
    • trimHeaderFields - Boolean - Should the header fields be trimmed?
      • Default: false
    • unwindArrays - Boolean - Should array values be "unwound" such that there is one line per value in the array?
      • Default: false
      • Example:
      [
          {
              "_id": {"$oid": "5cf7ca3616c91100018844af"},
              "data": {"category": "Computers", "options": [{"name": "MacBook Pro 15"}, {"name": "MacBook Air 13"}]}
          },
          {
              "_id": {"$oid": "5cf7ca3616c91100018844bf"},
              "data": {"category": "Cars", "options": [{"name": "Supercharger"}, {"name": "Turbocharger"}]}
          }
      ]
      
      • true will unwind the JSON to four objects, and therefore four lines of CSV values:
      _id.$oid,data.category,data.options.name
      5cf7ca3616c91100018844af,Computers,MacBook Pro 15
      5cf7ca3616c91100018844af,Computers,MacBook Air 13
      5cf7ca3616c91100018844bf,Cars,Supercharger
      5cf7ca3616c91100018844bf,Cars,Turbocharger
      
      • false will leave the values unwound and will convert the array as-is (when this option is used without expandArrayObjects):
      _id.$oid,data.category,data.options
      5cf7ca3616c91100018844af,Computers,"[{""name"":""MacBook Pro 15""},{""name"":""MacBook Air 13""}]"
      5cf7ca3616c91100018844bf,Cars,"[{""name"":""Supercharger""},{""name"":""Turbocharger""}]"
      
      • Note: This may result in CSV output that does not map back exactly to the original JSON.
    • useLocaleFormat - Boolean - Should values be converted to a locale specific string?
      • Default: false
      • Note: If selected, values will be converted using toLocaleString() rather than toString()

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 CSV
  • callback - A function of the form function (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 delimiters
      • field - String - Field Delimiter.
        • Default: ,
      • wrap - String - The character that field values are wrapped in.
        • Default: "
      • eol - String - End of Line Delimiter.
        • Default: \n
    • 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
    • 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.
    • trimHeaderFields - Boolean - Should the header fields be trimmed?
      • Default: false
    • trimFieldValues - Boolean - Should the field values be trimmed?
      • Default: false

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.

CLI

Note: As of 3.5.8, the command line interface functionality has been pulled out to a separate package. Please be sure to install the @mrodrig/json-2-csv-cli NPM package if you wish to use the CLI functionality shown below:

$ npm install @mrodrig/json-2-csv-cli

json2csv

Usage: json2csv <jsonFile> [options]

Options:
  -V, --version                    output the version number
  -o, --output [output]            Path of output file. If not provided, then stdout will be used
  -f, --field <delimiter>          Optional field delimiter
  -w, --wrap <delimiter>           Optional wrap delimiter
  -e, --eol <delimiter>            Optional end of line delimiter
  -b, --excel-bom                  Excel Byte Order Mark character prepended to CSV
  -W, --without-header             Withhold the prepended header
  -s, --sort-header                Sort the header fields
  -H, --trim-header                Trim header fields
  -F, --trim-fields                Trim field values
  -S, --check-schema               Check for schema differences
  -E, --empty-field-value <value>  Empty field value
  -A, --expand-array-objects       Expand array objects
  -k, --keys [keys]                Keys of documents to convert to CSV
  -h, --help                       output usage information

csv2json

Usage: csv2json <csvFile> [options]

Options:
  -V, --version            output the version number
  -c, --csv <csv>          Path of json file to be converted
  -o, --output [output]    Path of output file. If not provided, then stdout will be used
  -f, --field <delimiter>  Optional field delimiter
  -w, --wrap <delimiter>   Optional wrap delimiter
  -e, --eol <delimiter>    Optional end of line delimiter
  -b, --excel-bom          Excel Byte Order Mark character prepended to CSV
  -H, --trim-header        Trim header fields
  -F, --trim-fields        Trim field values
  -k, --keys [keys]        Keys of documents to convert to CSV
  -h, --help               output usage information

Tests

$ npm test

To see test coverage, please run:

$ npm run coverage

Current Coverage is:

Statements   : 100% ( 286/286 )
Branches     : 100% ( 166/166 )
Functions    : 100% ( 73/73 )
Lines        : 100% ( 280/280 )

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)
  • Document schema verification functionality (field order is irrelevant) (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.
  • 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
  • Empty field value option (as of 3.1.0)
  • TypeScript typings included (as of 3.4.0) - thanks to @GabrielCastro!

Current Tags

  • 3.0.0-beta.0                                ...           beta (2 years ago)
  • 3.7.5                                ...           latest (17 days ago)

88 Versions

  • 3.7.5                                ...           17 days ago
  • 3.7.4                                ...           19 days ago
  • 3.7.3                                ...           20 days ago
  • 3.7.2                                ...           23 days ago
  • 3.7.1                                ...           a month ago
  • 3.7.0                                ...           2 months ago
  • 3.6.2                                ...           3 months ago
  • 3.6.1                                ...           4 months ago
  • 3.6.0                                ...           5 months ago
  • 3.5.9                                ...           7 months ago
  • 3.5.8                                ...           7 months ago
  • 3.5.7                                ...           9 months ago
  • 3.5.6                                ...           a year ago
  • 3.5.5                                ...           a year ago
  • 3.5.4                                ...           a year ago
  • 3.5.3                                ...           a year ago
  • 3.5.2                                ...           a year ago
  • 3.5.0                                ...           a year ago
  • 3.4.2                                ...           a year ago
  • 3.4.1                                ...           a year ago
  • 3.4.0                                ...           a year ago
  • 3.3.0                                ...           a year ago
  • 3.2.0                                ...           a year ago
  • 3.1.1                                ...           2 years ago
  • 3.1.0                                ...           2 years ago
  • 3.0.0                                ...           2 years ago
  • 3.0.0-beta.0                                ...           2 years ago
  • 2.4.0 [deprecated]           ...           2 years ago
  • 2.3.1 [deprecated]           ...           2 years ago
  • 2.3.0 [deprecated]           ...           2 years ago
  • 2.2.1 [deprecated]           ...           2 years ago
  • 2.2.0 [deprecated]           ...           2 years ago
  • 2.1.2 [deprecated]           ...           3 years ago
  • 2.1.1 [deprecated]           ...           3 years ago
  • 2.1.0 [deprecated]           ...           4 years ago
  • 2.0.23 [deprecated]           ...           4 years ago
  • 2.0.22 [deprecated]           ...           4 years ago
  • 2.0.21 [deprecated]           ...           4 years ago
  • 2.0.20 [deprecated]           ...           4 years ago
  • 2.0.19 [deprecated]           ...           4 years ago
  • 2.0.18 [deprecated]           ...           4 years ago
  • 2.0.12 [deprecated]           ...           5 years ago
  • 2.0.11 [deprecated]           ...           5 years ago
  • 2.0.10 [deprecated]           ...           5 years ago
  • 2.0.9 [deprecated]           ...           5 years ago
  • 2.0.8 [deprecated]           ...           5 years ago
  • 2.0.7 [deprecated]           ...           5 years ago
  • 2.0.6 [deprecated]           ...           5 years ago
  • 2.0.5 [deprecated]           ...           5 years ago
  • 2.0.4 [deprecated]           ...           5 years ago
  • 2.0.3 [deprecated]           ...           5 years ago
  • 2.0.2 [deprecated]           ...           5 years ago
  • 2.0.1 [deprecated]           ...           5 years ago
  • 2.0.0 [deprecated]           ...           5 years ago
  • 1.5.0 [deprecated]           ...           5 years ago
  • 1.4.0 [deprecated]           ...           5 years ago
  • 1.3.1 [deprecated]           ...           5 years ago
  • 1.3.0 [deprecated]           ...           5 years ago
  • 1.2.1 [deprecated]           ...           5 years ago
  • 1.2.0 [deprecated]           ...           5 years ago
  • 1.1.3 [deprecated]           ...           5 years ago
  • 1.1.2 [deprecated]           ...           5 years ago
  • 1.1.1 [deprecated]           ...           5 years ago
  • 1.1.0 [deprecated]           ...           5 years ago
  • 1.0.8 [deprecated]           ...           5 years ago
  • 1.0.7 [deprecated]           ...           5 years ago
  • 1.0.6 [deprecated]           ...           6 years ago
  • 0.1.5 [deprecated]           ...           6 years ago
  • 0.1.4 [deprecated]           ...           6 years ago
  • 0.1.3 [deprecated]           ...           6 years ago
  • 0.1.2 [deprecated]           ...           6 years ago
  • 0.1.1 [deprecated]           ...           6 years ago
  • 0.1.0 [deprecated]           ...           6 years ago
  • 0.0.19 [deprecated]           ...           6 years ago
  • 0.0.18 [deprecated]           ...           6 years ago
  • 0.0.17 [deprecated]           ...           6 years ago
  • 0.0.16 [deprecated]           ...           6 years ago
  • 0.0.15 [deprecated]           ...           6 years ago
  • 0.0.14 [deprecated]           ...           6 years ago
  • 0.0.13 [deprecated]           ...           6 years ago
  • 0.0.12 [deprecated]           ...           6 years ago
  • 0.0.11 [deprecated]           ...           6 years ago
  • 0.0.10 [deprecated]           ...           6 years ago
  • 0.0.9 [deprecated]           ...           6 years ago
  • 0.0.8 [deprecated]           ...           6 years ago
  • 0.0.3 [deprecated]           ...           6 years ago
  • 0.0.2 [deprecated]           ...           6 years ago
  • 0.0.1 [deprecated]           ...           6 years ago

Copyright 2014 - 2016 © taobao.org |