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

csv2json-editor

v3.6.6

Published

Easily convert CSV files to JSON format with options to select specific columns, customize column names, and more.

Downloads

5

Readme

csv2json-editor

Easily convert CSV files to JSON format with options to select specific columns, customize column names, and more.

Features

  • Output JSON to a specified file path
  • Return JSON objects directly
  • Option to include or exclude header
  • Select specific columns for conversion
  • Customize column titles for output

Installation

npm i csv2json-editor

Versions

  • 3.6.6
    • Output JSON to a specified file path
    • Return JSON objects directly
    • Option to include or exclude header
    • Select specific columns for conversion
    • Customize column titles for output
  • 1.0.0
    • Test release version.

Usage

For JSON file output

import csv2jsonEditor from "csv2json-editor";

const options = {
    path: "./data/data.csv",
    to: "./data.json",
    header: false,
    columns: ["1", "3"],
    titles: ["name", "url"]
};

csv2jsonEditor(options);

For JSON objects return

import csv2jsonEditor from "csv2json-editor";

const options = {
    path: "./data/data.csv",
    to: false, // Set to false
    header: false,
    columns: ["1", "3"],
    titles: ["name", "url"]
};

csv2jsonEditor(options)
    .then(res => console.log(res))
    .catch(err => console.log(err));

Parameters

  • path

    • Description: File path where your CSV file exists.
    • Default: path = "./"
    • Example: ./data/yourFileName.csv
  • to

    For JSON file output:

    • Description: File path where you want to save your JSON file.
    • Default: to = "./data.json"
    • Example: ./output/NameYourJSONfile.json

    For JSON object return:

    • Description: Set to to false to return JSON objects instead of saving to a file.
    • Default: to = false
    • Example: See usage example
  • header

    • Description: Option to include or exclude the header.
    • Default: header = false
    • Example:
      • If your CSV file has a header, and you want to include it, set header = true.
      • If your CSV file doesn't have a header, ensure header = true.
  • columns

    • Description: Array of column indices to include.
    • Default: columns = []
    • Note: If you want to keep all columns, omit the columns parameter.
    • Example: If your CSV file has six columns, and you want to keep columns 2, 3, and 6, set columns = [1, 2, 5].
    • Indexing: Indices start at 0.
  • titles

    • Description: Array of custom names for the columns in the JSON output.
    • Default: titles = []
    • Note: If you want to keep the original column names, omit the titles parameter.
    • Example: Ensure the length of the titles array matches the number of columns in your CSV file OR the length of columns parameter.
    • Indexing: Indices start at 0.

Support

Your generous donation allows me to work on more open-source projects.

ko-fi

License

MIT License

Examples

Let's say your CSV file is located in the files folder:

id,name,age,city,country,occupation
1,John Doe,30,New York,USA,Engineer
2,Jane Smith,25,Los Angeles,USA,Designer
3,Michael Brown,45,Chicago,USA,Manager
4,Linda Johnson,35,Houston,USA,Developer
5,David Wilson,28,Phoenix,USA,Analyst

You want to:

  • Extract only two columns: name and occupation.
  • Rename these columns to JsonName and JsonOccupation.
  • Save the JSON output file in the root directory with the name myJsonFile.json.

Here is how you can implement this using the csv2json-editor:

import csv2jsonEditor from "csv2json-editor";

const options = {
    path: "./files/data.csv",
    to: "./myJsonFile.json",
    header: false,
    columns: ["1", "5"],
    titles: ["JsonName", "JsonOccupation"]
};

csv2jsonEditor(options);

Result of myJsonFile.json file:

[
  {
    "JsonName": "John Doe",
    "JsonOccupation": "Engineer"
  },
  {
    "JsonName": "Jane Smith",
    "JsonOccupation": "Designer"
  },
  {
    "JsonName": "Michael Brown",
    "JsonOccupation": "Manager"
  },
  {
    "JsonName": "Linda Johnson",
    "JsonOccupation": "Developer"
  },
  {
    "JsonName": "David Wilson",
    "JsonOccupation": "Analyst"
  }
]

Logs

  • Tue May 14, 2024
    • (5.14.01) - Added documentation. Published version 3.6.6.
  • Mon May 13, 2024
    • (5.13.01) - Fixed index issue error.
    • (5.13.02) - Added custom titles parameter. But facing one issue.
    • (5.13.03) - Fixed custom titles issue.
    • (5.13.04) - Changed parameter order.
    • (5.13.05) - Added file output feature.
    • (5.13.06) - Commited for safety.
    • (5.13.07) - All features are working now.
    • (5.13.08) - Cleaned codes.
    • (5.13.09) - Improved code efficient.
  • Sun May 12, 2024
    • (5.12.01) - Committed for safety.
    • (5.12.02) - If the users don't define the column index, it will automatically grasp all the columns.
    • (5.12.03) - All functions are now working.
    • (5.12.04) - Created getJson function for code efficient.
    • (5.12.05) - Cleaned codes.
  • Tue May 7, 2024
    • (5.7.01) - Read the csv file from the directory. Added path and header parameters.
    • (5.7.02) - Committed for safety.
    • (5.7.03) - Converting csv file to json file is now working with three parameters (path, columns, header).
  • Mon May 6, 2024
    • (first-commit) - Started this project 🔥. Released test version 1.0.0 as package as testing.