npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

csv-tools

v1.2.0

Published

Convert CSV to JSON and JSON to CSV.

Downloads

471

Readme

CSV Tools

Node module to convert CSV to JSON and JSON to CSV.

Build Status

CSV Tools does a great job exporting and importing data. Javascript objects can easily be exported to CSV, a format that non-programmers can easily understand and modify. On the other hand, CSV data can be imported into your appliation using CSV Tools and easily use that data as JSON objects.

This libraries provides 3 methods:

CSV to JSON

CSV Tools can parse any CSV file or string to an array of JSONs. The CVS's first row must contain the headers and the JSON properties will be named as the header for the corresponding column.

For example, the following CSV:

first_name,last_name,gender
Dennis,Anderson,Male
Peter,Hicks,Male

will be converted into:

[
  {
    "first_name": "Dennis",
    "last_name": "Anderson",
    "gender": "Male"
  },
  {
    "first_name": "Peter",
    "last_name": "Hicks",
    "gender": "Male"
  }
]

A dot (.) in a header will create a subdocument. For example:

device,owner.name.first,owner.name.last,owner.gender
94-0B-05-0E-E9-35,Pamela,Hart,Female
69-71-86-FD-B1-24,Karen,Pierce,Female

will be converted into:

[
  {
    "device": "94-0B-05-0E-E9-35",
    "owner": {
      "name": {
        "first": "Pamela",
        "last": "Hart"
      },
      "gender": "Female"
    }
  },
  {
    "device": "69-71-86-FD-B1-24",
    "owner": {
      "name": {
        "first": "Karen",
        "last": "Pierce"
      },
      "gender": "Female"
    }
  }
]

JSON to CSV

In the same way, JSONs can be converted into CSV. The properties will be stored as headers in the first row of the CSV. If the JSON has subdocuments, its properties will be attached to the corresponding header with a dot between the parent property and its children. For example:

[
  {
    "age": 21,
    "name": {
      "first": "Sylvia",
      "last": "Mcfarland"
    },
    "company": "AMRIL",
    "phone": "+1 (919) 503-3392"
  },
  {
    "age": 21,
    "name": {
      "first": "Adkins",
      "last": "Becker"
    },
    "company": "COMTREK",
    "phone": "+1 (938) 564-3477"
  }
]

will be converted into:

age,name.first,name.last,company,phone
21,"Sylvia","Mcfarland","AMRIL","+1 (919) 503-3392"
21,"Adkins","Becker","COMTREK","+1 (938) 564-3477"

If any of the objects in the array does not have a property, that value will be empty in the resulting CSV.

Input:

[
  {
    "age": 21,
    "phone": "+1 (919) 503-3392",
    "name": "Sylvia Mcfarland"
  },
  {
    "age": 34,
    "name": "Adkins Becker",
  }
]

Output:

age,phone,name
21,"+1 (919) 503-3392","Sylvia Mcfarland"
34,,"Adkins Becker"

Install

$ npm install csv-tools

Usage

To use this module:

var csv = require('csv-tools');

csv.toJSON(csvData[, delimiter])

Convert csvData to JSON.

delimiter refers to the character used to separate CVS columns. Comma (,) by default.

var csv = require('csv-tools');

var csvData = "first_name,last_name,gender\nDennis,Anderson,Male\nPeter,Hicks,Male";

var json = csv.toJSON(csvData);

console.log(JSON.stringify(json, null, 2));

csv.fileToJSON(csvFilePath[, delimiter], callback)

Convert the CSV file in csvFilePath to JSON. The callback gets one argument (data) where data is a JSON with the data in the CSV file.

delimiter refers to the character used to separate CVS columns. Comma (,) by default.

var csv = require('csv-tools');

var file = "data.csv";

csv.fileToJSON(file, function(json) {
  console.log(JSON.stringify(json,null,2));
});

csv.fromJSON(jsonArray[, delimiter])

Convert jsonArray to CSV.

delimiter refers to the character used to separate CVS columns. Comma (,) by default.

var csv = require('csv-tools');

var jsonArray = [
  {
    "first_name": "Dennis",
    "last_name": "Anderson",
    "gender": "Male"
  },
  {
    "first_name": "Peter",
    "last_name": "Hicks",
    "gender": "Male"
  }
];

var csvData = csv.fromJSON(jsonArray);

console.log(csvData);