Nominally Patriotic Meathead

    mk-json-2-csv

    3.0.2 • Public • Published

    json-2-csv

    Convert JSON to CSV or CSV to JSON

    Dependencies Downloads NPM version Known Vulnerabilities Package Size

    Build Status Maintainability Test Coverage Donate

    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 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:
      • 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
      • 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
      • sortHeader - Boolean - Should the header keys be sorted in alphabetical order?
        • Default: false
      • trimHeaderFields - Boolean - Should the header fields be trimmed?
        • Default: false
      • trimFieldValues - Boolean - Should the field values be trimmed? (in development)
        • Default: false
      • 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.
      • 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.

    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
      • trimHeaderFields - Boolean - Should the header fields be trimmed?
        • Default: false
      • trimFieldValues - Boolean - Should the field values be trimmed?
        • 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.

    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

    Install

    npm i mk-json-2-csv

    DownloadsWeekly Downloads

    12

    Version

    3.0.2

    License

    MIT

    Unpacked Size

    49.3 kB

    Total Files

    16

    Last publish

    Collaborators

    • d925529