New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

npm_csv_to_json

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

npm_csv_to_json

A npm package for CSV to JSON converter and vice versa

  • 1.0.1
  • latest
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

CSVJSONDataType

CSVJSONDataType is a simple utility class for managing CSV data with JavaScript. It provides methods to upload CSV files, set headers, add rows, delete rows, convert data back to CSV format, and retrieve rows and headers.

Installation

npm i npm_csv_to_json

To use CSVJSONDataType in your project, first include it in your JavaScript file:

import { CSVJSONDataType } from "npm_csv_to_json";

Usage

Initializing and Uploading a CSV File

You can initialize a CSVJSONDataType instance and upload a CSV file as follows:

const a = new CSVJSONDataType();
await a.uploadCSV("./output.csv");
console.log(a.head, a.row);

Setting Headers and Adding Rows

You can also initialize CSVJSONDataType with an existing header, replace the current header, and add rows:

const a = new CSVJSONDataType(["Apple"]);
a.setHead(["apple", "ball", "cat"]);
a.addRow({ apple: 1, ball: 2 })
  .addRow({ apple: 5, Ball: 6 })
  .addRow({ cat: 10, ball: 12 });

Deleting a Row

You can delete a row by specifying its index:

a.deleteRow(0);

Converting Data to CSV

To convert the current data back to CSV format:

a.convertToCSV();

Retrieving Rows and Headers

You can retrieve all rows and headers as follows:

console.log(a.getRows());
console.log(a.getHead());

Example

Here is a full example demonstrating all the features:

import { CSVJSONDataType } from "npm_csv_to_json";

// Initialize with a header
const a = new CSVJSONDataType();

// Set a new header
a.setHead(["apple", "ball", "cat"]);

// Add rows
a.addRow({ apple: 1, ball: 2 })
  .addRow({ apple: 5, Ball: 6 })
  .addRow({ cat: 10, ball: 12 });

// Delete the first row
a.deleteRow(0);

// Convert to CSV
a.convertToCSV();

// Retrieve rows and headers
console.log(a.getRows()); // Output the rows
console.log(a.getHead()); // Output the headers

License

This project is licensed under the MIT License. See the LICENSE file for details.

css

Feel free to adjust the paths and additional details according to your specific proj

Keywords

FAQs

Package last updated on 11 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc