What is json2csv?
The json2csv npm package is a powerful tool for converting JSON data to CSV format. It is widely used for data transformation and export tasks, making it easier to handle JSON data in a tabular format.
What are json2csv's main functionalities?
Convert JSON to CSV
This feature allows you to convert a JSON array into a CSV string. The code sample demonstrates how to use the `parse` function from the json2csv package to transform a JSON array into CSV format.
const { parse } = require('json2csv');
const json = [{ "name": "John", "age": 30 }, { "name": "Jane", "age": 25 }];
const csv = parse(json);
console.log(csv);
Customizing CSV Fields
This feature allows you to specify which fields to include in the CSV output. The code sample shows how to define a list of fields and pass them as options to the `parse` function.
const { parse } = require('json2csv');
const json = [{ "name": "John", "age": 30 }, { "name": "Jane", "age": 25 }];
const fields = ['name', 'age'];
const opts = { fields };
const csv = parse(json, opts);
console.log(csv);
Handling Nested JSON Objects
This feature allows you to handle nested JSON objects and flatten them into CSV format. The code sample demonstrates how to specify nested fields in the options to correctly map them to CSV columns.
const { parse } = require('json2csv');
const json = [{ "name": "John", "address": { "city": "New York", "state": "NY" } }, { "name": "Jane", "address": { "city": "San Francisco", "state": "CA" } }];
const fields = ['name', 'address.city', 'address.state'];
const opts = { fields };
const csv = parse(json, opts);
console.log(csv);
Other packages similar to json2csv
csv-writer
The csv-writer package provides a simple and flexible way to write CSV files. It supports writing both objects and arrays to CSV, and allows for customization of headers and field delimiters. Compared to json2csv, csv-writer focuses more on writing CSV files rather than converting JSON to CSV strings.
fast-csv
The fast-csv package is a comprehensive library for parsing and formatting CSV files. It offers high performance and a wide range of features, including support for streaming and handling large datasets. While json2csv is primarily focused on converting JSON to CSV, fast-csv provides more extensive functionality for working with CSV data in general.
papaparse
The papaparse package is a powerful CSV parser that can handle large files and supports various configurations for parsing and formatting. It is known for its speed and reliability. Unlike json2csv, which is mainly used for converting JSON to CSV, papaparse excels at parsing CSV files into JSON and other formats.
:warning: WARNING: THIS PACKAGE IS ABANDONED
The code has moved to a new home.
This repository stays as a the historic home of json2csv
up until v5.
From v6, the library has been broken into smaller libraries that are now published to NPM independently:
- Plainjs: Includes the
Parser
API and a new StreamParser
API which doesn't the conversion in a streaming fashion in pure js. - Node: Includes the
Node Transform
and Node Async Parser
APIs for Node users. - WHATWG: Includes the
WHATWG Transform Stream
and WHATWG Async Parser
APIs for users of WHATWG streams (browser, Node or Deno). - CLI: Includes the
CLI
interface. - Transforms: Includes the built-in
transforms
for json2csv. - Formatters: Includes the built-in
formatters
for json2csv. Formatters are the new way to format data before adding it to the resulting CSV.
Up-to-date documentation of the library can be found at https://juanjodiaz.github.io/json2csv