jsonexport

Travis Known Vulnerabilities NPM Version NPM Downloads NPM Downloads NPM License GitHub stars Try jsonexport on RunKit

This module makes easy to convert JSON to CSV and its very customizable.

Project Page

Online Demo Page

Table of Contents - [Usage](#usage) - [CLI](#cli) - [Browser](#browser) - [Browser Import Examples](#browser-import-examples) - [Stream](#stream) - [JSON Array Example](#json-array-example) - [Simple Array](#simple-array) - [JSON Object Example](#json-object-example) - [Options](#options) - [typeHandlers](#typehandlers) - [Contributors](#contributors)

Usage

Installation command is npm install jsonexport.

Run tests with npm test.

var jsonexport = require('jsonexport');

jsonexport({lang: 'Node.js',module: 'jsonexport'}, {rowDelimiter: '|'}, function(err, csv){
    if(err) return console.log(err);
    console.log(csv);
});

CLI

Global installation command is npm install -g jsonexport.

Convert JSON to CSV using cat data.json | jsonexport or jsonexport data.json

Usage: jsonexport <JSON filename> <CSV filename>

Browser

Use the code in the folder named dist to run jsonexport in the browser

Browser Import Examples

Webpack

var jsonexport = require("jsonexport/dist")

Typescript

import * as jsonexport from "jsonexport/dist"

Stream

var jsonexport = require('jsonexport');
var fs = require('fs');

var reader = fs.createReadStream('data.json');
var writer = fs.createWriteStream('out.csv');

reader.pipe(jsonexport()).pipe(writer);

JSON Array Example

Simple Array

Code

var jsonexport = require('jsonexport');

var contacts = [{
    name: 'Bob',
    lastname: 'Smith'
},{
    name: 'James',
    lastname: 'David'
},{
    name: 'Robert',
    lastname: 'Miller'
},{
    name: 'David',
    lastname: 'Martin'
}];

jsonexport(contacts,function(err, csv){
    if(err) return console.log(err);
    console.log(csv);
});

Result

name,lastname
Bob,Smith
James,David
Robert,Miller
David,Martin

Complex Array

Code

var jsonexport = require('jsonexport');

var contacts = [{
   name: 'Bob',
   lastname: 'Smith',
   family: {
       name: 'Peter',
       type: 'Father'
   }
},{
   name: 'James',
   lastname: 'David',
   family:{
       name: 'Julie',
       type: 'Mother'
   }
},{
   name: 'Robert',
   lastname: 'Miller',
   family: null,
   location: [1231,3214,4214]
},{
   name: 'David',
   lastname: 'Martin',
   nickname: 'dmartin'
}];

jsonexport(contacts,function(err, csv){
    if(err) return console.log(err);
    console.log(csv);
});

Result

name,lastname,family.name,family.type,family,location,nickname
Bob,Smith,Peter,Father
James,David,Julie,Mother
Robert,Miller,,,,1231;3214;4214
David,Martin,,,,,dmartin

JSON Object Example

Simple Object

Code

var jsonexport = require('jsonexport');

var stats = {
    cars: 12,
    roads: 5,
    traffic: 'slow'
};

jsonexport(stats,function(err, csv){
    if(err) return console.log(err);
    console.log(csv);
});

Result

cars,12
roads,5
traffic,slow

Complex Object

Code

var jsonexport = require('jsonexport');

var stats = {
    cars: 12,
    roads: 5,
    traffic: 'slow',
    speed: {
        max: 123,
        avg: 20,
        min: 5
    },
    size: [10,20]
};

jsonexport(stats,function(err, csv){
    if(err) return console.log(err);
    console.log(csv);
});

Result

cars,12
roads,5
traffic,slow
speed.max,123
speed.avg,20
speed.min,5
size,10;20

Options

In order to get the most of out of this module, you can customize many parameters and functions.

Deprecated Options (Use typeHandlers)

typeHandlers

Define types by constructors and what function to run when that type is matched

var jsonexport = require('jsonexport');

//data
var contacts = {
  'a' : Buffer.from('a2b', 'utf8'),
  'b' : Buffer.from('other field', 'utf8'),
  'x' : 22,
  'z' : function(){return 'bad ace'}
};

var options={
  //definitions to type cast
  typeHandlers:{
    Array:function(value,index,parent){
      return 'replaced-array';
    },
    Boolean:function(value,index,parent){
      return 'replaced-boolean';
    },
    Function:function(value,index,parent){
      return value()
    },
    Number:function(value,index,parent){
      return 'replaced-number';
    },
    String:function(value,index,parent){
      return 'replaced-string';
    },
    Buffer:function(value,index,parent){
      return value.toString()
    }
  }
}

jsonexport(contacts, options, function(err, csv){
  console.log( csv )
});

The output would be:

a,a2b
b,other field
x,replaced-number
z,bad ace

When using typeHandlers, Do NOT do this

var options={
  typeHandlers:{
    Object:function(value,index,parent){
      return 'EVERYTHING IS AN OBJECT';
    }
  }
}

It is NOT an error, however the recursive result becomes illegable functionality strings

Contributors