Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce formatters #455

Merged
merged 1 commit into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"es6": true
},
"parserOptions": {
"ecmaVersion": 8
"ecmaVersion": 9
},
"extends": "eslint:recommended"
}
880 changes: 329 additions & 551 deletions README.md

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions bin/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const readFile = promisify(readFileOrig);
const writeFile = promisify(writeFileOrig);

const { unwind, flatten } = json2csv.transforms;
const { string: stringFormatterCtr, stringExcel: stringExcelFormatter } = json2csv.formatters;
const JSON2CSVParser = json2csv.Parser;
const Json2csvTransform = json2csv.Transform;

Expand Down Expand Up @@ -149,18 +150,24 @@ async function processStream(config, opts) {
separator: config.flattenSeparator
}));
}

const stringFormatter = stringFormatterCtr({
quote: config.quote,
escapedQuote: config.escapedQuote,
});
const formatters = {
string: config.excelStrings ? stringExcelFormatter({ stringFormatter }) : stringFormatter
};

const opts = {
transforms,
formatters,
fields: config.fields
? (Array.isArray(config.fields) ? config.fields : config.fields.split(','))
: config.fields,
defaultValue: config.defaultValue,
quote: config.quote,
escapedQuote: config.escapedQuote,
delimiter: config.delimiter,
eol: config.eol,
excelStrings: config.excelStrings,
header: config.header,
includeEmptyRows: config.includeEmptyRows,
withBOM: config.withBom
Expand Down
77 changes: 77 additions & 0 deletions docs/cli-examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# CLI examples

All examples use this example [input file](https://github.com/zemirco/json2csv/blob/master/test/fixtures/json/default.json).

## Input file and specify fields

```sh
$ json2csv -i input.json -f carModel,price,color
carModel,price,color
"Audi",10000,"blue"
"BMW",15000,"red"
"Mercedes",20000,"yellow"
"Porsche",30000,"green"
```

## Input file, specify fields and use pretty logging

```sh
$ json2csv -i input.json -f carModel,price,color -p
```

![Screenshot](https://s3.amazonaws.com/zeMirco/github/json2csv/json2csv-pretty.png)

## Generating CSV containing only specific fields

```sh
$ json2csv -i input.json -f carModel,price,color -o out.csv
$ cat out.csv
carModel,price,color
"Audi",10000,"blue"
"BMW",15000,"red"
"Mercedes",20000,"yellow"
"Porsche",30000,"green"
```

Same result will be obtained passing the fields config as a file.

```sh
$ json2csv -i input.json -c fieldsConfig.json -o out.csv
```

where the file `fieldsConfig.json` contains

```json
[
"carModel",
"price",
"color"
]
```

## Read input from stdin

```sh
$ json2csv -f price
[{"price":1000},{"price":2000}]
```

Hit <kbd>Enter</kbd> and afterwards <kbd>CTRL</kbd> + <kbd>D</kbd> to end reading from stdin. The terminal should show

```sh
price
1000
2000
```

## Appending to existing CSV

Sometimes you want to add some additional rows with the same columns.
This is how you can do that.

```sh
# Initial creation of csv with headings
$ json2csv -i test.json -f name,version > test.csv
# Append additional rows
$ json2csv -i test.json -f name,version --no-header >> test.csv
```
Loading