Coder Social home page Coder Social logo

geovanerocha / jsonexport Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kaue/jsonexport

0.0 1.0 0.0 189 KB

:page_facing_up: Makes easy to convert JSON to CSV

Home Page: http://cnova.github.io/jsonexport

License: Apache License 2.0

JavaScript 100.00%

jsonexport's Introduction

jsonexport

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

Project Page

Changelog

v1.0.6 - Create new lines in the CSV file to handle JSON objects with arrays

Usage

Installation command is npm install jsonexport.

var jsonexport = require('jsonexport');

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

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

lastname;name;family.type;family.name;nickname;location
Smith;Bob;Father;Peter;;
David;James;Mother;Julie;;
Miller;Robert;;;;1231,3214,4214
Martin;David;;;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

Customization

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

####Options

  • headerPathString - String Used to create the propriety path, defaults to . example contact: {name: 'example} = contact.name
  • rowDelimiter - String Change the file row delimiter, defaults to , for cvs format. \t for xls format.
  • endOfLine - String Replace the OS default EOL.
  • mainPathItem - String Every header will have the mainPathItem as the base.
  • arrayPathString - String This is used to output primitive arrays in a single column, defaults to ;
  • booleanTrueString - String Will be used instead of true.
  • booleanFalseString - String Will be used instead of false.
  • includeHeaders - Boolean Set this option to false to hide the CSV headers.
  • orderHeaders - Boolean By default the most used columns are shown first.
  • undefinedString - String If you want to display a custom value for undefined strings, use this option. Defaults to .
  • verticalOutput - Boolean Set this option to false to create a horizontal output for JSON Objects, headers in the first row, values in the second.
  • handleString - Function Use this to customize all Strings in the CSV file.
  • handleNumber - Function Use this to customize all Numbers in the CSV file.
  • handleBoolean - Function Use this to customize all Booleans in the CSV file.
  • handleDate - Function Use this to customize all Dates in the CSV file.
  • handleArray - Function Use this to customize all Arrays in the CSV file.
  • handleObject - Function Use this to customize all Objects in the CSV file.

Handle Customization

Lets say you want to prepend a text to every string in your CSV file, how to do it?

var jsonexport = require('jsonexport');

var options = {
    handleString: function(string, name){
        return 'Hey - ' + string;
    }
};

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

The output would be:

lang;Hey - Node.js
module;Hey - jsonexport

jsonexport's People

Contributors

kaue avatar kauegimenes avatar

Watchers

Geovane Rocha 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.