Skip to content

Commit 55b6633

Browse files
committed
Initial release of json-2-csv-cli.
0 parents  commit 55b6633

File tree

8 files changed

+348
-0
lines changed

8 files changed

+348
-0
lines changed

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Coverage directory used by tools like istanbul
2+
coverage
3+
.nyc_output
4+
5+
# Dependency directory
6+
node_modules
7+
8+
# Users Environment Configurations
9+
.idea
10+
11+
# Debug logs
12+
npm-debug.log

.npmignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
test/
2+
.git*
3+
npm-debug.log
4+
.travis.yml
5+
.eslintrc
6+
node_modules
7+
coverage
8+
.nyc_output
9+
upgrade_guides
10+
tslint.json
11+
_config.yml

README.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# json-2-csv-cli
2+
**Convert JSON to CSV _or_ CSV to JSON**
3+
4+
This module provides the command line interface functionality for the [json-2-csv](https://www.npmjs.org/package/json-2-csv) package.
5+
6+
[![Dependencies](https://img.shields.io/david/mrodrig/json-2-csv-cli.svg)](https://www.npmjs.org/package/json-2-csv-cli)
7+
[![Downloads](http://img.shields.io/npm/dm/json-2-csv-cli.svg)](https://www.npmjs.org/package/json-2-csv-cli)
8+
[![NPM version](https://img.shields.io/npm/v/json-2-csv-cli.svg)](https://www.npmjs.org/package/json-2-csv-cli)
9+
[![Known Vulnerabilities](https://snyk.io/test/npm/json-2-csv-cli/badge.svg)](https://snyk.io/test/npm/json-2-csv-cli)
10+
[![Package Size](https://img.shields.io/bundlephobia/min/json-2-csv-cli.svg)](https://www.npmjs.org/package/json-2-csv-cli)
11+
12+
## Installation
13+
14+
CLI:
15+
```bash
16+
$ npm install json-2-csv-cli
17+
```
18+
19+
## Usage
20+
### json2csv
21+
```
22+
Usage: json2csv <jsonFile> [options]
23+
24+
Options:
25+
-V, --version output the version number
26+
-o, --output [output] Path of output file. If not provided, then stdout will be used
27+
-f, --field <delimiter> Optional field delimiter
28+
-w, --wrap <delimiter> Optional wrap delimiter
29+
-e, --eol <delimiter> Optional end of line delimiter
30+
-b, --excel-bom Excel Byte Order Mark character prepended to CSV
31+
-W, --without-header Withhold the prepended header
32+
-s, --sort-header Sort the header fields
33+
-H, --trim-header Trim header fields
34+
-F, --trim-fields Trim field values
35+
-S, --check-schema Check for schema differences
36+
-E, --empty-field-value <value> Empty field value
37+
-A, --expand-array-objects Expand array objects
38+
-k, --keys [keys] Keys of documents to convert to CSV
39+
-h, --help output usage information
40+
```
41+
42+
### csv2json
43+
```
44+
Usage: csv2json <csvFile> [options]
45+
46+
Options:
47+
-V, --version output the version number
48+
-c, --csv <csv> Path of json file to be converted
49+
-o, --output [output] Path of output file. If not provided, then stdout will be used
50+
-f, --field <delimiter> Optional field delimiter
51+
-w, --wrap <delimiter> Optional wrap delimiter
52+
-e, --eol <delimiter> Optional end of line delimiter
53+
-b, --excel-bom Excel Byte Order Mark character prepended to CSV
54+
-H, --trim-header Trim header fields
55+
-F, --trim-fields Trim field values
56+
-k, --keys [keys] Keys of documents to convert to CSV
57+
-h, --help output usage information
58+
```

bin/csv2json.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
const pkg = require('json-2-csv'),
6+
utils = require('./utils/utils'),
7+
program = require('commander');
8+
9+
program
10+
.version(pkg.version)
11+
.usage('<csvFile> [options]')
12+
.option('-c, --csv <csv>', 'Path of json file to be converted', utils.readInputFile)
13+
.option('-o, --output [output]', 'Path of output file. If not provided, then stdout will be used', utils.convertToAbsolutePath)
14+
.option('-f, --field <delimiter>', 'Optional field delimiter')
15+
.option('-w, --wrap <delimiter>', 'Optional wrap delimiter')
16+
.option('-e, --eol <delimiter>', 'Optional end of line delimiter')
17+
.option('-b, --excel-bom', 'Excel Byte Order Mark character prepended to CSV')
18+
.option('-H, --trim-header', 'Trim header fields')
19+
.option('-F, --trim-fields', 'Trim field values')
20+
.option('-k, --keys [keys]', 'Keys of documents to convert to CSV', utils.constructKeysList, undefined)
21+
.parse(process.argv);
22+
23+
Promise.resolve({
24+
csv: utils.readInputFile(program.args && program.args.length && program.args[0]),
25+
output: program.output,
26+
options: {
27+
delimiter: {
28+
field: program.field,
29+
wrap: program.wrap,
30+
eol: program.eol
31+
},
32+
excelBOM: Boolean(program.excelBom),
33+
trimHeaderFields: Boolean(program.trimHeader),
34+
trimFieldValues: Boolean(program.trimFields),
35+
keys: program.keys
36+
}
37+
})
38+
.then(utils.parseInputFiles)
39+
.then(utils.determineConverter)
40+
.then(utils.performConversion)
41+
.then(utils.processOutput);

bin/json2csv.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
const pkg = require('json-2-csv'),
6+
utils = require('./utils/utils'),
7+
program = require('commander');
8+
9+
program
10+
.version(pkg.version)
11+
.usage('<jsonFile> [options]')
12+
.option('-o, --output [output]', 'Path of output file. If not provided, then stdout will be used', utils.convertToAbsolutePath)
13+
.option('-f, --field <delimiter>', 'Optional field delimiter')
14+
.option('-w, --wrap <delimiter>', 'Optional wrap delimiter')
15+
.option('-e, --eol <delimiter>', 'Optional end of line delimiter')
16+
.option('-b, --excel-bom', 'Excel Byte Order Mark character prepended to CSV')
17+
.option('-W, --without-header', 'Withhold the prepended header')
18+
.option('-s, --sort-header', 'Sort the header fields')
19+
.option('-H, --trim-header', 'Trim header fields')
20+
.option('-F, --trim-fields', 'Trim field values')
21+
.option('-S, --check-schema', 'Check for schema differences')
22+
.option('-E, --empty-field-value <value>', 'Empty field value')
23+
.option('-A, --expand-array-objects', 'Expand array objects')
24+
.option('-k, --keys [keys]', 'Keys of documents to convert to CSV', utils.constructKeysList, undefined)
25+
.parse(process.argv);
26+
27+
Promise.resolve({
28+
json: utils.readInputFile(program.args && program.args.length && program.args[0]),
29+
output: program.output,
30+
options: {
31+
delimiter: {
32+
field: program.field,
33+
wrap: program.wrap,
34+
eol: program.eol
35+
},
36+
excelBOM: Boolean(program.excelBom),
37+
prependHeader: !program.withoutHeader,
38+
sortHeader: Boolean(program.sortHeader),
39+
trimHeaderFields: Boolean(program.trimHeader),
40+
trimFieldValues: Boolean(program.trimFields),
41+
checkSchemaDifferences: Boolean(program.checkSchema),
42+
expandArrayObjects: Boolean(program.expandArrayObjects),
43+
emptyFieldValue: program.emptyFieldValue,
44+
keys: program.keys
45+
}
46+
})
47+
.then(utils.parseInputFiles)
48+
.then(utils.determineConverter)
49+
.then(utils.performConversion)
50+
.then(utils.processOutput);

bin/utils/utils.js

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
'use strict';
2+
3+
const path = require('path'),
4+
fs = require('fs'),
5+
converter = require('../../../json-2-csv');
6+
7+
module.exports = {
8+
constructKeysList,
9+
readInputFile,
10+
convertToAbsolutePath,
11+
parseInputFiles,
12+
determineConverter,
13+
performConversion,
14+
processOutput
15+
};
16+
17+
function convertToAbsolutePath(filePath) {
18+
if (filePath && !path.isAbsolute(filePath)) {
19+
return path.join(process.cwd(), filePath);
20+
}
21+
return filePath;
22+
}
23+
24+
function readInputFile(filePath) {
25+
filePath = convertToAbsolutePath(filePath);
26+
return fs.readFileSync(filePath).toString();
27+
}
28+
29+
function parseInputFiles(params) {
30+
if (params.json) {
31+
params.json = JSON.parse(params.json);
32+
}
33+
return params;
34+
}
35+
36+
function determineConverter(params) {
37+
if (params.json) {
38+
params.conversion = 'json2csv';
39+
params.converter = converter.json2csvAsync;
40+
} else if (params.csv) {
41+
params.conversion = 'csv2json';
42+
params.converter = converter.csv2jsonAsync;
43+
} else {
44+
return Promise.reject('No data provided for conversion');
45+
}
46+
47+
return params;
48+
}
49+
50+
function performConversion(params) {
51+
switch (params.conversion) {
52+
case 'json2csv':
53+
return params.converter(params.json, params.options)
54+
.then((outputData) => {
55+
params.outputData = outputData;
56+
return params;
57+
});
58+
case 'csv2json':
59+
return params.converter(params.csv, params.options)
60+
.then((outputData) => {
61+
params.outputData = outputData;
62+
return params;
63+
});
64+
default:
65+
return Promise.reject('Invalid conversion specified.');
66+
}
67+
}
68+
69+
function writeToFile(filePath, data) {
70+
fs.writeFileSync(filePath, data);
71+
}
72+
73+
function processOutput(params) {
74+
if (params.output && params.json) {
75+
// Write the raw output data when converting from JSON to CSV
76+
return writeToFile(params.output, params.outputData);
77+
} else if (params.output) {
78+
// Pretty print the output when converting from CSV to JSON
79+
return writeToFile(params.output, JSON.stringify(params.outputData, null, 4));
80+
}
81+
// Otherwise, no output was specified so just send it to stdout via the console
82+
console.log(params.outputData); // eslint-disable-line no-console
83+
}
84+
85+
function constructKeysList(key, keys) {
86+
// Initialize as empty array, if undefined at start
87+
if (!keys) {
88+
keys = [];
89+
}
90+
91+
keys.push(key);
92+
return keys;
93+
}

package-lock.json

+41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"author": {
3+
"name": "Mike Rodrigues",
4+
"email": "[email protected]"
5+
},
6+
"name": "json-2-csv-cli",
7+
"description": "",
8+
"version": "3.5.8",
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/mrodrig/json-2-csv-cli.git"
12+
},
13+
"bugs": {
14+
"url": "https://github.com/mrodrig/json-2-csv-cli/issues"
15+
},
16+
"bin": {
17+
"json2csv": "./bin/json2csv.js",
18+
"csv2json": "./bin/csv2json.js"
19+
},
20+
"keywords": [
21+
"json",
22+
"to",
23+
"csv",
24+
"converter",
25+
"export",
26+
"json2csv",
27+
"csv2json",
28+
"parse",
29+
"parser",
30+
"json-2-csv",
31+
"csv-2-json",
32+
"cli"
33+
],
34+
"dependencies": {
35+
"commander": "2.20.0",
36+
"json-2-csv": "*"
37+
},
38+
"engines": {
39+
"node": ">= 8"
40+
},
41+
"license": "MIT"
42+
}

0 commit comments

Comments
 (0)