npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mk-json-2-csv

v3.0.2

Published

A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.

Downloads

16

Readme

json-2-csv

Convert JSON to CSV or CSV to JSON

Dependencies Downloads NPM version Known Vulnerabilities Package Size

Build Status Maintainability Test Coverage Donate

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.

Installation

$ npm install json-2-csv

Usage

let converter = require('json-2-csv');

Upgrading?

Upgrading to v3 from v2? Check out the upgrade guide.

API

converter.json2csv(array, callback, options)

  • 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 the following key value pairs:
    • delimiter - Document - Specifies the different types of delimiters
      • field - String - Field Delimiter.
        • Default: ,
      • wrap - String - Wrap values in the delimiter of choice (e.g. wrap values in quotes).
        • Default: "
      • eol - String - End of Line Delimiter.
        • Default: \n
    • excelBOM - Boolean - Should a unicode character be prepended to allow Excel to open a UTF-8 encoded file with non-ASCII characters present.
    • prependHeader - Boolean - Should the auto-generated header be prepended as the first line in the CSV?
      • Default: true
    • sortHeader - Boolean - Should the header keys be sorted in alphabetical order?
      • Default: false
    • trimHeaderFields - Boolean - Should the header fields be trimmed?
      • Default: false
    • trimFieldValues - Boolean - Should the field values be trimmed? (in development)
      • Default: false
    • checkSchemaDifferences - Boolean - Should all documents have the same schema?
      • Default: false
      • Note: Change this to false if some documents are missing certain fields and you still want to convert the data.
    • keys - Array - Specify the keys (as strings) that should be converted.
      • Default: null
      • If you have a nested object (ie. {info : {name: 'Mike'}}), then set this to ['info.name']
      • If you want all keys to be converted, then specify null or don't specify the option to utilize the default.

For examples, please refer to the json2csv API Documentation (Link)

Promisified Version: converter.json2csvAsync(array, options)

Available in version 2.2.0, this functionality makes use of promises from the bluebird module.

converter.csv2json(csv, callback, options)

  • csv - A string of CSV
  • callback - 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 the following key value pairs:
    • delimiter - Document - Specifies the different types of delimiters
      • field - String - Field Delimiter.
        • Default: ,
      • wrap - String - The character that field values are wrapped in.
        • Default: "
      • eol - String - End of Line Delimiter.
        • Default: \n
    • excelBOM - Boolean - Does the CSV contain a unicode character prepended in order to allow Excel to open a UTF-8 encoded file with non-ASCII characters present?
      • Default: false
    • trimHeaderFields - Boolean - Should the header fields be trimmed?
      • Default: false
    • trimFieldValues - Boolean - Should the field values be trimmed?
      • Default: false
    • keys - Array - Specify the keys (as strings) that should be converted.
      • Default: null
      • If you have a nested object (ie. {info : {name: 'Mike'}}), then set this to ['info.name']
      • If you want all keys to be converted, then specify null or don't specify the option to utilize the default.

For examples, please refer to the csv2json API Documentation (Link)

Promisified Version: csv2jsonAsync(csv, options)

Available in version 2.2.0, this functionality makes use of promises from the bluebird module.

Tests

$ npm test

Note: This requires mocha, should, and underscore.

To see test coverage, please run:

$ npm run coverage

Current Coverage is:

Statements   : 100% ( 258/258 )
Branches     : 100% ( 124/124 )
Functions    : 100% ( 49/49 )
Lines        : 100% ( 256/256 )

Frequently Asked Questions (FAQ)

Please find the updated list (relocated to the Wiki) here: Frequently Asked Questions (Link)

Features

  • Header Generation (per document keys)
  • Allows for conversion of specific keys in both json2csv and csv2json via the options.keys parameter (as of 1.1.2)
  • Verifies all documents have same schema (schema field order does not matter as of 1.1.0)
  • Supports sub-documents natively
  • Supports arrays as document values for both json2csv and csv2json
  • Custom ordering of columns (see F.A.Q. for more information)
  • Ability to re-generate the JSON documents that were used to generate the CSV (including nested documents)
  • Allows for custom field delimiters, end of line delimiters, etc.
  • Promisifiable via bluebird's .promisify() and .promisifyAll() (as of 1.1.1)
  • Wrapped value support for json2csv and csv2json (as of 1.3.0)
  • Support for multiple different schemas (as of 1.4.0)
  • Promisified versions of the functions are now available by default: json2csvAsync, csv2jsonAsync (as of 2.2.0)
  • RFC 4180 Compliance (as of 3.0.0)
  • CLI functionality (as of 3.0.0)
    • csv2json test.csv -o output.json
    • and
    • json2csv test.json -o output.csv -W -k arrayOfStrings -o output.csv