Socket
Socket
Sign inDemoInstall

json-2-csv

Package Overview
Dependencies
Maintainers
1
Versions
140
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-2-csv - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

.idea/.name

3

lib/json-2-csv.js

@@ -64,2 +64,5 @@ 'use strict';

if (!data) { throw new Error('Cannot call json2csv on ' + data); }
if (typeof data === 'object' && !data.length) { // Single document, not an array
data = [data]; // Convert to an array of the given document
}
async.parallel([retrieveHeading(data), generateCsv(data)], function (err, res) {

@@ -66,0 +69,0 @@ if (!err) {

18

package.json

@@ -5,3 +5,3 @@ {

"description": "A JSON to CSV converter that natively supports sub-documents and auto-generates the heading.",
"version": "0.1.0",
"version": "0.1.1",
"repository": {

@@ -11,3 +11,6 @@ "type": "git",

},
"main": "./lib/json-2-csv.js",
"main": "./lib/converter.js",
"scripts": {
"test": "./node_modules/.bin/mocha --reporter spec"
},
"keywords": [

@@ -18,4 +21,7 @@ "json",

"json2csv",
"csv2json",
"json2csv-converter",
"json-2-csv"
"csv2json-converter",
"json-2-csv",
"csv-2-json"
],

@@ -26,3 +32,7 @@ "dependencies": {

},
"devDependencies": {},
"devDependencies": {
"mocha": "~1.14.0",
"should": "~2.0.2",
"async": "~0.2.9"
},
"engines": {

@@ -29,0 +39,0 @@ "node": "*"

@@ -1,7 +0,135 @@

This is a node script that will let you convert JSON into a CSV string.
# Convert JSON to CSV or CSV to JSON
TODO:
-Setup GIT repo
-Setup NPM structure
-Add to NPM
-Update README with install, usage, tests, etc.
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
```bash
$ npm install json-2-csv
```
## Usage
```javascript
var converter = require('json-2-csv');
```
### API
#### json2csv(array, callback)
* `array` - An array of JSON documents
* `callback` - A function of the form `function (err, csv)`; This function will receive any errors and/or the CSV generated.
##### json2csv Example:
```javascript
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:
```csv
Make,Model,Year,Specifications.Mileage,Specifications.Trim
Nissan,Murano,2013,7106,S AWD
BMW,X5,2014,3287,M
```
#### csv2json(csv, callback)
* `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.
##### csv2json Example:
```javascript
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:
```text
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' } } ]
```
## Tests
```bash
$ npm test
```
_Note_: This requires `mocha`, `should`, and `async`.
## Features
- Header Generation (per document keys)
- Verifies all documents have same schema
- Supports sub-documents natively
- Custom ordering of columns (see F.A.Q. for more information)
## F.A.Q.
- Can the order of the keys be changed in the output?
__Yes.__ Currently, changing the order of the keys in the JSON document will also change the order of the columns. (Node 10.26)
## TODO
- Add more tests
- Get csv2json under test
- Add more documentation
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