Code Monkey home page Code Monkey logo

tabson's Introduction

tabson

Convert a tab file (TSV) to JSON

npm npm

Install

Install the package using NPM:

npm install --save tabson

Usage

//Import dependencies
var fs = require('fs');
var tabson = require('tabson');

// file.txt content:
// col1	col2	col3	col4
// element1.1	element1.2	element1.3	element1.4
// element2.1	element2.2	element2.3	element2.4
// element3.1	element3.2	element3.3	element3.4

//Read the test tab file
tabson('./file.txt', { type: 'object', sep: '\t' }, function(error, header, data)
{
  //Check for error
  if(error){ return console.error(error.message); }

  //Save the file
  fs.writeFileSync('./file.json', JSON.stringify(data), 'utf8');

  // file.json content:
  // [
  //   {"col1":"element1.1","col2":"element1.2","col3":"element1.3","col4":"element1.4"},
  //   {"col1":"element2.1","col2":"element2.2","col3":"element2.3","col4":"element2.4"},
  //   {"col1":"element3.1","col2":"element3.2","col3":"element3.3","col4":"element3.4"}
  // ]
});

tabson(file, options, callback)

Read the content of the file and return his content in a JSON format.

file

A string with the path of the file to read.

options (optionally)

An object with the following options:

  • encoding: set the encoding. Default: utf8.
  • chunk: set the chunk size. Default is 4098.
  • sep: set the file separator. Default is a tabulation \t.
  • empty: set the value for the empty columns. Default is an empty string.
  • type: set the output type:
    • array: each row of the table will be saved as an array object.
    • object: each row of the table will he saved as an object with the format column_name : column_content.

callback

A function that will be called when the file is full read. This function will be called with the following arguments:

  • error: an error object.
  • header: an array with the column names.
  • data: an array with the table content.

Related

License

MIT LICENSE © Josemi Juanes.

tabson's People

Contributors

jmjuanes avatar

Watchers

James Cloos avatar  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.