Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
json-2-csv
Advanced tools
A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.
The json-2-csv npm package is a utility for converting JSON data to CSV format and vice versa. It is useful for data transformation tasks, especially when dealing with data interchange between systems that use different formats.
Convert JSON to CSV
This feature allows you to convert an array of JSON objects into a CSV string. The code sample demonstrates how to use the json2csv function to transform JSON data into CSV format.
const json2csv = require('json-2-csv');
const json = [
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Anna', age: 22, city: 'London' },
{ name: 'Mike', age: 32, city: 'Chicago' }
];
json2csv.json2csv(json, (err, csv) => {
if (err) {
throw err;
}
console.log(csv);
});
Convert CSV to JSON
This feature allows you to convert a CSV string into an array of JSON objects. The code sample demonstrates how to use the csv2json function to transform CSV data into JSON format.
const json2csv = require('json-2-csv');
const csv = 'name,age,city\nJohn,30,New York\nAnna,22,London\nMike,32,Chicago';
json2csv.csv2json(csv, (err, json) => {
if (err) {
throw err;
}
console.log(json);
});
Custom Delimiters
This feature allows you to specify custom delimiters for fields and arrays when converting JSON to CSV. The code sample demonstrates how to use the json2csv function with custom delimiter options.
const json2csv = require('json-2-csv');
const json = [
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Anna', age: 22, city: 'London' },
{ name: 'Mike', age: 32, city: 'Chicago' }
];
const options = {
delimiter: {
field: ';',
array: '|'
}
};
json2csv.json2csv(json, (err, csv) => {
if (err) {
throw err;
}
console.log(csv);
}, options);
The csv-parser package is a streaming CSV parser that converts CSV data into JSON objects. It is highly efficient for processing large CSV files. Unlike json-2-csv, which provides both JSON to CSV and CSV to JSON conversion, csv-parser focuses solely on parsing CSV to JSON.
The papaparse package is a powerful CSV parser that can handle large files and supports web workers for asynchronous parsing. It offers both CSV to JSON and JSON to CSV conversion, similar to json-2-csv, but with additional features like streaming and chunking for large datasets.
The fast-csv package is a comprehensive library for parsing and formatting CSV data. It supports both reading from and writing to CSV files, similar to json-2-csv. However, fast-csv is designed for high performance and can handle large datasets efficiently.
This node module will convert an array of JSON documents to a CSV string. Column headings will be automatically generated based on the keys of the JSON documents. Nested documents will have a '.' appended between the keys.
It is also capable of converting CSV of the same form back into the original array of JSON documents. The columns headings will be used as the JSON document keys. All lines must have the same exact number of CSV values.
$ npm install json-2-csv
var converter = require('json-2-csv');
array
- An array of JSON documents to be converted to CSV.callback
- A function of the form function (err, csv)
; This function will receive any errors and/or the string of CSV generated.options
- (Optional) A JSON document specifying any of {DELIMITER
, EOL
, PARSE_CSV_NUMBERS
}
DELIMITER
- Document - Specifies the different types of delimiters
FIELD
- String - Field Delimiter. Default: ','
ARRAY
- String - Array Value Delimiter. Default: ';'
WRAP
- String - Wrap values in the delimiter of choice (e.g. wrap values in quotes). Default: null
EOL
- String - End of Line Delimiter. Default: '\n'
PARSE_CSV_NUMBERS
- Boolean - Should numbers that are found in the CSV be converted to numbers? Default: false
var converter = require('json-2-csv');
var documents = [
{
Make: 'Nissan',
Model: 'Murano',
Year: '2013',
Specifications: {
Mileage: '7106',
Trim: 'S AWD'
}
},
{
Make: 'BMW',
Model: 'X5',
Year: '2014',
Specifications: {
Mileage: '3287',
Trim: 'M'
}
}
];
var json2csvCallback = function (err, csv) {
if (err) throw err;
console.log(csv);
};
converter.json2csv(documents, json2csvCallback);
The above code prints out:
Make,Model,Year,Specifications.Mileage,Specifications.Trim
Nissan,Murano,2013,7106,S AWD
BMW,X5,2014,3287,M
csv
- A string of CSVcallback
- A function of the form function (err, array)
; This function will receive any errors and/or the array of JSON documents generated.options
- (Optional) A JSON document specifying any of {DELIMITER
, EOL
, PARSE_CSV_NUMBERS
}
DELIMITER
- Document - Specifies the different types of delimiters
FIELD
- String - Field Delimiter. Default: ','
ARRAY
- String - Array Value Delimiter. Default: ';'
EOL
- String - End of Line Delimiter. Default: '\n'
PARSE_CSV_NUMBERS
- Boolean - Should numbers that are found in the CSV be converted to numbers? Default: false
var converter = require('json-2-csv');
var csv = "Make,Model,Year,Specifications.Mileage,Specifications.Trim\n" +
"Nissan,Murano,2013,7106,S AWD\n" +
"BMW,X5,2014,3287,M\n";
var csv2jsonCallback = function (err, json) {
if (err) throw err;
console.log(typeof json);
console.log(json.length);
console.log(json);
}
converter.csv2json(csv, csv2jsonCallback);
The above code prints out:
object
2
[ { Make: 'Nissan',
Model: 'Murano',
Year: '2013',
Specifications: { Mileage: '7106', Trim: 'S AWD' } },
{ Make: 'BMW',
Model: 'X5',
Year: '2014',
Specifications: { Mileage: '3287', Trim: 'M' } } ]
$ npm test
Note: This requires mocha
, should
, async
, and underscore
.
FAQs
A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.
The npm package json-2-csv receives a total of 367,494 weekly downloads. As such, json-2-csv popularity was classified as popular.
We found that json-2-csv demonstrated a healthy version release cadence and project activity because the last version was released less than 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
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.