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

csv2jsonfile

v1.2.3

Published

A Tool convert CSV to JSON or JS Object with no dependencies.

Downloads

15

Readme

csv2jsonfile

Build StatusCodecovnpmnpm bundle size

A Tool convert CSV to JSON or JS Object with NO Dependencies.

Install

npm install --save csv2jsonfile

or

yarn add csv2jsonfile

see from npm package.

Usage

note: the csv file must have header(used as key), like the example below:

city,level,alias
Shanghai,3,SH
Chongqing,3,SQ
Jinan,4,JN 

CSV to JSON

const csv2jsonfile = require('csv2jsonfile');
const path = require('path');
const sourcePath = path.resolve(__dirname, 'data.csv'); 
const targetPath = path.resolve(__dirname, 'result.json');

// default convert CSV with header
csv2jsonfile(sourcePath, targetPath)
  .then((time) => {
    console.log(`use: ${time} ms`);
  })
  .catch((err) => {
    console.log(err.message);
  })
// result in result.json
/*
[
  {
    city: "Shangehai",
    level: "3",
    alias: "SH"
  },
  {
    city: "Chongqing",
    level: "3",
    alias: "CQ"
  },
]
*/
// CSV without header
csv2jsonfile(sourcePath, targetPath, { header: false })
  .then((time) => {
    console.log(`use: ${time} ms`);
  })
  .catch((err) => {
    console.log(err.message);
  })

CSV to JavaScript Object

var csv2jsonfile = require('csv2jsonfile');
csv2jsonfile.inline('data.csv')
  .then((obj) => {
    console.log(obj)
  })
  .catch((err) => {
    console.log(err);
  })
// CSV without header
csv2jsonfile.inline('data.csv', { header: false })
  .then((obj) => {
    console.log(obj);
  })
  .catch((err) => {
    console.log(err);
  })

API

/**
 * convert csv to json file
 * @param {string} sourcePath absolute path of csv file 
 * @param {string} targetPath absolute path of json file
 * @param {object} options default { header: true }
 * @return {Promise} ms usage through convert progress
 */
csv2jsonfile(sourcePath, targetPath, options?)

/**
 *  convert csv to javascript object
 * @param {string} sourcePath absolute path of csv file
 * @param {object} options default { header: true }
 * @return {Promise} the object after convert
 */
csv2jsonfile.inline(sourcePath, options?)

ChangeLog

v1.2.0

  1. Delete targetPath file if it exists before convert.(no need to manuallt clear)
  2. add No Header CSV support.

If the CSV without header, It will be converted into Array each line. E.g:

// data.csv => data.json
// 1. without header
[
  ["Shanghai", "3", "SH"],
  ["Chongqing", "3", "CQ"],
  ["Jinan", "4", "JN"]
]
// with header
[
  {
    "city": "Shanghai",
    "level": "3",
    "alias": "SH"
  },
  {
    "city": "Chongqing",
    "level": "3",
    "alias": "CQ"
  },
  {
    "city": "Jinan",
    "level": "4",
    "alias": "JN"
  }
]
  1. fix bug: if there are no new empty line(\n) at the end of the file, may cause confusion.