Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
objects-to-csv
Advanced tools
Converts an array of objects into a CSV file. Saves CSV to disk or returns as string.
The objects-to-csv npm package is a simple and efficient tool for converting JavaScript objects into CSV (Comma-Separated Values) format. It is particularly useful for exporting data to CSV files, which can then be used for data analysis, reporting, or sharing with other systems.
Convert Objects to CSV String
This feature allows you to convert an array of JavaScript objects into a CSV string. The code sample demonstrates how to create a CSV string from an array of objects.
const ObjectsToCsv = require('objects-to-csv');
const data = [
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Jane', age: 25, city: 'San Francisco' }
];
const csv = new ObjectsToCsv(data);
const csvString = await csv.toString();
console.log(csvString);
Save CSV to File
This feature allows you to save the CSV string to a file on disk. The code sample demonstrates how to write the CSV data to a file named 'data.csv'.
const ObjectsToCsv = require('objects-to-csv');
const data = [
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Jane', age: 25, city: 'San Francisco' }
];
const csv = new ObjectsToCsv(data);
await csv.toDisk('./data.csv');
console.log('CSV file saved.');
Append Data to Existing CSV File
This feature allows you to append new data to an existing CSV file. The code sample demonstrates how to append a new object to the existing 'data.csv' file.
const ObjectsToCsv = require('objects-to-csv');
const newData = [
{ name: 'Alice', age: 28, city: 'Los Angeles' }
];
const csv = new ObjectsToCsv(newData);
await csv.toDisk('./data.csv', { append: true });
console.log('Data appended to CSV file.');
The json2csv package is another popular tool for converting JSON data to CSV format. It offers a variety of options for customizing the output, such as specifying fields, field names, and delimiters. Compared to objects-to-csv, json2csv provides more flexibility and customization options but may require more configuration.
The csv-writer package is designed for writing CSV files in a simple and efficient manner. It supports writing both objects and arrays to CSV files and offers features like custom headers and field ordering. Compared to objects-to-csv, csv-writer is more focused on writing CSV files and provides a straightforward API for this purpose.
The fast-csv package is a comprehensive library for working with CSV files, including reading, writing, and parsing CSV data. It is known for its performance and flexibility, making it suitable for handling large datasets. Compared to objects-to-csv, fast-csv offers a broader range of functionalities beyond just converting objects to CSV.
Converts an array of JavaScript objects into the CSV format. You can save the CSV to file or return it as a string.
The keys in the first object of the array will be used as column names.
Any special characters in the values (such as commas) will be properly escaped.
const ObjectsToCsv = require('objects-to-csv');
// Sample data - two columns, three rows:
const data = [
{code: 'CA', name: 'California'},
{code: 'TX', name: 'Texas'},
{code: 'NY', name: 'New York'},
];
// If you use "await", code must be inside an asynchronous function:
(async () => {
const csv = new ObjectsToCsv(data);
// Save to file:
await csv.toDisk('./test.csv');
// Return the CSV file as string:
console.log(await csv.toString());
})();
There are two methods, toDisk(filename)
and toString()
.
Converts the data and saves the CSV file to disk. The filename
must include the
path as well.
The options
is an optional parameter which is an object that contains the
settings. Supported options:
append
- whether to append to the file. Default is false
(overwrite the file).
Set to true
to append. Column names will be added only once at the beginning
of the file. If the file does not exist, it will be created.bom
- whether to add the Unicode Byte Order Mark at the beginning of the
file. Default is false
; set to true
to be able to view Unicode in Excel
properly. Otherwise Excel will display Unicode incorrectly.allColumns
- whether to check all array items for keys to convert to columns rather
than only the first. This will sort the columns alphabetically. Default is false
;
set to true
to check all items for potential column names.const ObjectsToCsv = require('objects-to-csv');
const sampleData = [{ id: 1, text: 'this is a test' }];
// Run asynchronously, without awaiting:
new ObjectsToCsv(sampleData).toDisk('./test.csv');
// Alternatively, you can append to the existing file:
new ObjectsToCsv(sampleData).toDisk('./test.csv', { append: true });
// `allColumns: true` collects column names from all objects in the array,
// instead of only using the first one. In this case the CSV file will
// contain three columns:
const mixedData = [
{ id: 1, name: 'California' },
{ id: 2, description: 'A long description.' },
];
new ObjectsToCsv(mixedData).toDisk('./test.csv', { allColumns: true });
Returns the CSV file as a string.
Two optional parameters are available:
header
controls whether the column names will be
returned as the first row of the file. Default is true
. Set it to false
to
get only the data rows, without the column names.allColumns
controls whether to check every item for potential keys to process,
rather than only the first item; this will sort the columns alphabetically by key name.
Default is false
. Set it to true
to process keys that may not be present
in the first object of the array.const ObjectsToCsv = require('objects-to-csv');
const sampleData = [{ id: 1, text: 'this is a test' }];
async function printCsv(data) {
console.log(
await new ObjectsToCsv(data).toString()
);
}
printCsv(sampleData);
Use Node.js version 8 or above.
1.3.6
FAQs
Converts an array of objects into a CSV file. Saves CSV to disk or returns as string.
The npm package objects-to-csv receives a total of 121,845 weekly downloads. As such, objects-to-csv popularity was classified as popular.
We found that objects-to-csv demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.