Giter Site home page Giter Site logo

tannguyen3489 / csvjson-csv2json Goto Github PK

View Code? Open in Web Editor NEW

This project forked from flatfilers/csvjson-csv2json

0.0 0.0 0.0 14 KB

Converts CSV to JSON. Powers the most used online tool CSVJSON. Used by thousands everyday.

Home Page: https://www.csvjson.com/

License: MIT License

JavaScript 92.32% HTML 7.68%

csvjson-csv2json's Introduction

CSVJSON csv2json() function

Single function csv2json to reliably convert CSV to JSON. Self contained without dependencies. Used to power CSVJSON the online tool found at www.csvjson.com/csv2json. Used by thousands everyday.

npm package here

Usage

Simply call csv2json passing a string to obtain JSON. The string may be CSV (comma separated values), TSV (tab separated values) or semi-colon separated values (typically used in French Excel). It will auto-detect the separator although you may override or force it via the separator option.

Node example

const csv2json = require('./csv2json.js');
const csv = `album, year, US_peak_chart_post
The White Stripes, 1999, -
De Stijl, 2000, -
White Blood Cells, 2001, 61
Elephant, 2003, 6
Get Behind Me Satan, 2005, 3
Icky Thump, 2007, 2
Under Great White Northern Lights, 2010, 11
Live in Mississippi, 2011, -
Live at the Gold Dollar, 2012, -
Nine Miles from the White City, 2013, -`;

const json = csv2json(csv, {parseNumbers: true});
console.log(json);

Browser example

Note: In the browser, global namespace CSVJSON is created. It contains the csv2json function.

<script type="text/javascript" src="csv2json.js"></script>
<script>
    const csv = `album, year, US_peak_chart_post
The White Stripes, 1999, -
De Stijl, 2000, -
White Blood Cells, 2001, 61
Elephant, 2003, 6
Get Behind Me Satan, 2005, 3
Icky Thump, 2007, 2
Under Great White Northern Lights, 2010, 11
Live in Mississippi, 2011, -
Live at the Gold Dollar, 2012, -
Nine Miles from the White City, 2013, -`;

    const json = CSVJSON.csv2json(csv, {parseNumbers: true});
    console.log(json);
</script>

In both cases, you would get this in the console:

[
  {
    "album": "The White Stripes",
    "year": 1999,
    "US_peak_chart_post": "-"
  },
  {
    "album": "De Stijl",
    "year": 2000,
    "US_peak_chart_post": "-"
  },
  {
    "album": "White Blood Cells",
    "year": 2001,
    "US_peak_chart_post": 61
  },
  {
    "album": "Elephant",
    "year": 2003,
    "US_peak_chart_post": 6
  },
  {
    "album": "Get Behind Me Satan",
    "year": 2005,
    "US_peak_chart_post": 3
  },
  {
    "album": "Icky Thump",
    "year": 2007,
    "US_peak_chart_post": 2
  },
  {
    "album": "Under Great White Northern Lights",
    "year": 2010,
    "US_peak_chart_post": 11
  },
  {
    "album": "Live in Mississippi",
    "year": 2011,
    "US_peak_chart_post": "-"
  },
  {
    "album": "Live at the Gold Dollar",
    "year": 2012,
    "US_peak_chart_post": "-"
  },
  {
    "album": "Nine Miles from the White City",
    "year": 2013,
    "US_peak_chart_post": "-"
  }
]

Documentation

csv2json supports a number of options passed as an optional hash:

  • separator: Character which acts as separator. If omitted, will attempt to detect comma ,, semi-colon ; or tab \t.
  • parseNumbers: If set to true will attempt to convert a value to a number, if possible.
  • parseJSON: If set to true will attempt to convert a value to a valid JSON value if possible. Detects numbers, null, false, true, [] and {}.
  • transpose: If set to true will pivot the table. The first column becomes the header.
  • hash: If set to true will use the first column as a key and return a hash instead of an array of objects.

You can of course test all of these options online on www.csvjson.com/csv2json.

Tests

Run the tests in your browser by opening test-browser.html.

Run the tests through node:

node test-node.js

Companion functions

json2csv to convert JSON to CSV. npm package here.

json_beautifier to beautify and format your JSON. npm package here.

JSON2_mod a replacement of JSON with more options to format your JSON. npm package here.

csvjson-csv2json's People

Contributors

martindrapeau avatar friederbluemle 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.