JSPM

  • Created
  • Published
  • Downloads 748216
  • Score
    100M100P100Q194146F

A JSON to CSV converter that natively supports sub-documents and auto-generates the heading.

Package Exports

  • json-2-csv

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (json-2-csv) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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

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

API

json2csv(array, callback)

  • array - An array of JSON documents
  • callback - A function of the form function (err, csv); This function will receive any errors and/or the CSV generated.
json2csv Example:
var converter = require('json-2-csv');

var documents = [
    {
        'Make': 'Nissan',
        'Model': 'Murano',
        'Year': '2013'
        'Specifications': {
            'Mileage': '7106',
            'Trim': 'S AWD'
        }
    },
    {
        'Make': 'BMW',
        'Model' 'X5',
        'Year': '2014',
        'Specifications': {
            'Mileage': '3287',
            'Trim': 'M'
        }
    }
];

var json2csvCallback = function (err, csv) {
    if (err) throw err;
    console.log(csv);
};

converter.json2csv(documents, json2csvCallback);

The above code prints out:

Make,Model,Year,Specifications.Mileage,Specifications.Trim
Nissan,Murano,2013,7106,S AWD
BMW,X5,2014,3287,M

csv2json(csv, callback)

  • 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.
csv2json Example:
var converter = require('json-2-csv');

var csv = "Make,Model,Year,Specifications.Mileage,Specifications.Trim\n" +
          "Nissan,Murano,2013,7106,S AWD\n" +
          "BMW,X5,2014,3287,M\n";

var csv2jsonCallback = function (err, json) {
    if (err) throw err;
    console.log(typeof json);
    console.log(json.length);
    console.log(json);
}

converter.csv2json(csv, csv2jsonCallback);

The above code prints out:

object
2
[ { Make: 'Nissan',
    Model: 'Murano',
    Year: '2013',
    Specifications: { Mileage: '7106', Trim: 'S AWD' } },
  { Make: 'BMW',
    Model: 'X5',
    Year: '2014',
    Specifications: { Mileage: '3287', Trim: 'M' } } ]

Tests

$ npm test

Note: This requires mocha, should, and async.

Features

  • Header Generation (per document keys)
  • Verifies all documents have same schema
  • Supports sub-documents natively
  • Custom ordering of columns (see F.A.Q. for more information)

F.A.Q.

  • Can the order of the keys be changed in the output? Yes. Currently, changing the order of the keys in the JSON document will also change the order of the columns. (Node 10.26)

TODO

  • Add more tests
  • Get csv2json under test
  • Add more documentation