All Projects โ†’ kaue โ†’ Jsonexport

kaue / Jsonexport

Licence: apache-2.0
{} โ†’ ๐Ÿ“„ it's easy to convert JSON to CSV

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Jsonexport

Singlespotify
๐ŸŽต Create Spotify playlists based on one artist through the command line
Stars: โœญ 254 (+22.12%)
Mutual labels:  cli, npm, npm-package, npm-module
Cpx
A cli tool to watch and copy file globs.
Stars: โœญ 394 (+89.42%)
Mutual labels:  cli, npm, npm-package, npm-module
Npm Run All
A CLI tool to run multiple npm-scripts in parallel or sequential.
Stars: โœญ 4,496 (+2061.54%)
Mutual labels:  cli, npm, npm-package, npm-module
Event Target Shim
An implementation of WHATWG EventTarget interface, plus few extensions.
Stars: โœญ 89 (-57.21%)
Mutual labels:  npm, npm-package, npm-module
Package.json
ๆ–‡ไปถ package.json ็š„่ฏดๆ˜Žๆ–‡ๆกฃใ€‚
Stars: โœญ 67 (-67.79%)
Mutual labels:  npm, npm-package, npm-module
Webcam Easy
javascript access webcam stream and take photo
Stars: โœญ 79 (-62.02%)
Mutual labels:  npm, npm-package, npm-module
Yarpm
CLI tool to run npm scripts with either npm or yarn, depending on how it was started
Stars: โœญ 13 (-93.75%)
Mutual labels:  cli, npm, npm-package
Darkmode.js
๐ŸŒ“ Add a dark-mode / night-mode to your website in a few seconds
Stars: โœญ 2,339 (+1024.52%)
Mutual labels:  npm, npm-package, npm-module
Tplink Cloud Api
A node.js npm module to remotely control TP-Link smartplugs (HS100, HS110) and smartbulbs (LB100, LB110, LB120, LB130) using their cloud web service (no need to be on the same wifi/lan)
Stars: โœญ 96 (-53.85%)
Mutual labels:  npm, npm-package, npm-module
Getme
CLI utility for everyday tasks. With getme you get weather, forecast, currency rate, upload files, IP address, word definitions, text translations, internet speed, do google searches, get inspirational quotes and get Chuck Norris jokes
Stars: โœญ 118 (-43.27%)
Mutual labels:  cli, npm, npm-package
Criterion
Microbenchmarking for Modern C++
Stars: โœญ 140 (-32.69%)
Mutual labels:  json, csv, export
Packagephobia
โš–๏ธ Find the cost of adding a new dependency to your project
Stars: โœญ 1,110 (+433.65%)
Mutual labels:  npm, npm-package, npm-module
Awesome Node Utils
some useful npm packages for nodejs itself
Stars: โœญ 51 (-75.48%)
Mutual labels:  npm, npm-package, npm-module
Forge Node App
๐Ÿ› ๐Ÿ“ฆ๐ŸŽ‰ Generate Node.js boilerplate with optional libraries & tools
Stars: โœญ 90 (-56.73%)
Mutual labels:  cli, npm-package, npm-module
Nls
Missing inspector for npm packages.
Stars: โœญ 44 (-78.85%)
Mutual labels:  cli, npm, npm-package
Cli
Get a programmable email address. Automate what happens when you receive emails. It's like Zapier for devs who hate emails.
Stars: โœญ 105 (-49.52%)
Mutual labels:  cli, json, npm
Ohshitgit
โ‰๏ธOh shit! A cli tool to help you unfuck your git mistakes
Stars: โœญ 135 (-35.1%)
Mutual labels:  cli, npm, npm-package
Homebridge Wol
A Wake on Lan plugin for Homebridge
Stars: โœญ 150 (-27.88%)
Mutual labels:  npm, npm-package, npm-module
Cash Cli
๐Ÿ’ฐ๐Ÿ’ฐ Convert currency rates directly from your terminal!
Stars: โœญ 168 (-19.23%)
Mutual labels:  cli, npm, npm-package
Np
A better `npm publish`
Stars: โœญ 6,401 (+2977.4%)
Mutual labels:  cli, npm, npm-package

jsonexport {} โ†’ ๐Ÿ“„

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

โœ” easy to use ๐Ÿ‘Œ (should work as expected without much customization)๏ธ

โœ” extendable ๐Ÿ•บ (many options to customize the output)

โœ”๏ธ tiny ๐Ÿœ (0 dependencies)

โœ” scalable ๐Ÿ’ช (works with big files using Streams)

โœ” fast โšก

Project Page

Online Demo Page

Table of Contents

Usage

Installation command is npm install jsonexport.

Run tests with npm test.

const jsonexport = require('jsonexport');

jsonexport({lang: 'Node.js', module: 'jsonexport'}, {rowDelimiter: '|'}, function(err, csv){
    if (err) return console.error(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

const jsonexport = require("jsonexport/dist")

Typescript

import * as jsonexport from "jsonexport/dist"

Stream

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

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

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

Promise

const jsonexport = require('jsonexport')
try {
    const csv = await jsonexport({lang: 'Node.js', module: 'jsonexport'}, {rowDelimiter: '|'});
} catch (err) {
    console.error(err);
}

JSON Array Example

Simple Array

Code

const jsonexport = require('jsonexport');

const 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.error(err);
    console.log(csv);
});

Result

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

Complex Array

Code

const jsonexport = require('jsonexport');

const 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.error(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

const jsonexport = require('jsonexport');

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

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

Result

cars,12
roads,5
traffic,slow

Complex Object

Code

const jsonexport = require('jsonexport');

const 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.error(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.

  • headerPathString - String Used to create the propriety path, defaults to . example contact: {name: 'example} = contact.name
  • fillGaps - Boolean Set this option if don't want to have empty cells in case of an object with multiple nested items (array prop), defaults to false Issue #22
  • fillTopRow - Boolean try filling top rows first for unpopular colums, defaults to false
  • headers - Array Used to set a custom header order, defaults to [] example ['lastname', 'name']
  • rename - Array Used to set a custom header text, defaults to [] example ['Last Name', 'Name']
  • mapHeaders - Function Post-process headers after they are calculated with delimiters, example mapHeaders: (header) => header.replace(/foo\./, '')
  • rowDelimiter - String Change the file row delimiter
    • Defaults to , (cvs format).
    • Use \t for xls format.
    • Use ; for (windows excel .csv format).
  • textDelimiter - String The character used to escape the text content if needed (default to ")
  • forceTextDelimiter - Boolean Set this option to true to wrap every data item and header in the textDelimiter. Defaults to false
  • 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.
  • 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.
  • typeHandlers - {typeName:(value, index, parent)=>any A key map of constructors used to match by instance to create a value using the defined function (see example)

typeHandlers

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

const jsonexport = require('jsonexport');

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

const 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) {
  if (err) return console.error(err);
  console.log(csv);
});

The output would be:

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

Date typeHandler?

var date = new Date();
jsonexport({
    a: date,
    b: true
}, {
    typeHandlers: {
        Object: (value, name) => {
            if (value instanceof Date) return date.toLocaleString();
            return value;
        }
    }
}, (err, csv) => {
    if (err) return console.error(err);
    console.log(csv);
});

When using typeHandlers, Do NOT do this

const 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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].