Giter VIP home page Giter VIP logo

json-2-csv's Introduction

Convert JSON to CSV or CSV to JSON

Build Status bitHound Dependencies Downloads NPM version bitHound Score

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

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

API

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: ','
      • array - String - Array Value 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'
    • 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
    • emptyFieldValue - String - Value for fields without data when not checking schemas.
      • Default: 'null'
    • 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: true
      • 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: json2csvPromisified(array, options).then(...).catch(...);

Available in version 2.2.0, this functionality makes use of promises from the bluebird module.

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: ','
      • array - String - Array Value Delimiter.
        • Default: ';'
      • wrap - String - The character that field values are wrapped in.
        • Default: ''
      • eol - String - End of Line Delimiter.
        • Default: '\n'
    • 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: csv2jsonPromisified(csv, options).then(...).catch(...);

Available in version 2.2.0, this functionality makes use of promises from the bluebird module.

Tests

$ npm test

Note: This requires mocha, should, async, and underscore.

To see test coverage, please run:

$ npm run coverage

Current Coverage is:

Statements   : 94.84% ( 202/213 )
Branches     : 93.33% ( 154/165 )
Functions    : 100% ( 37/37 )
Lines        : 95.52% ( 192/201 )

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: json2csvPromisified, csv2jsonPromisified (as of 2.2.0)

json-2-csv's People

Contributors

dillten avatar mrodrig avatar simonh1000 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.