csv2json-editor

3.6.6 • Public • Published

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.

Package Sidebar

Install

npm i csv2json-editor

Weekly Downloads

2

Version

3.6.6

License

MIT

Unpacked Size

9.57 kB

Total Files

4

Last publish

Collaborators

  • sithukhant