Command Line Interface to convert JSON to CSV.
npm install @json2csv/cli




Fast and highly configurable JSON to CSV converter.
It fully support conversion following the RFC4180 specification as well as other similar text delimited formats as TSV.
@json2csv/cli makes json2csv usable as a command line tool.
- Fast and lightweight
- Support for standard JSON as well as NDJSON
- Scalable to infinitely large datasets (using stream processing)
- Advanced data selection (automatic field discovery, underscore-like selectors, custom data getters, default values for missing fields, ...)
- Support for custom input data transformation
- Support for custom csv cell formatting.
- Highly customizable (supporting custom quotation marks, delimiters, eol values, etc.)
- Automatic escaping (preserving new lines, quotes, etc.)
- Optional headers
- Unicode encoding support
- Pretty printing in table format to stdout
There are multiple flavours of json2csv:
* 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.
And a couple of libraries that enable additional configurations:
* Transforms: Includes the built-in transforms for json2csv (unwind and flatten) allowing the using to transform data before is parsed.
* Formatters: Includes the built-in formatters for json2csv (one for each data type, an excel-specific one, etc.). Formatters convert JSON data types into CSV-compatible strings.
- Node v16+
You can install json2csv as a dependency using NPM.
It's advisable to install it as a global dependency so it can be called from anywhere.
``bash`
$ npm install -g @json2csv/cli
You can install json2csv using Yarn.
It's advisable to install it as a global dependency so it can be called from anywhere.
`bash`
$ yarn global add @json2csv/cli
`bash`
$ json2csv -i input.json
`bash
Usage: json2csv [options]
Options:
-V, --version output the version number
-i, --input Path and name of the incoming json file. Defaults to stdin.
-o, --output
#### Input file, data selection and output to stdout
`bash
$ 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, data selection and pretty output to stdout
`bash
$ json2csv -i input.json -f carModel,price,color -p
┌────────────────────┬───────────────┬───────────────┐
│ "carModel" │ "price" │ "color" │
├────────────────────┼───────────────┼───────────────┤
│ "Audi" │ 10000 │ "blue" │
├────────────────────┼───────────────┼───────────────┤
│ "BMW" │ 15000 │ "red" │
├────────────────────┼───────────────┼───────────────┤
│ "Mercedes" │ 20000 │ "yellow" │
├────────────────────┼───────────────┼───────────────┤
│ "Porsche" │ 30000 │ "green" │
└────────────────────┴───────────────┴───────────────┘
`
#### Input file, data selection and output to file
`bash
$ 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.
`bash`
$ json2csv -i input.json -c config.json -o out.csv
where the file config.json contains
`json`
{ "fields": ["carModel", "price", "color"] }
#### Input from stdin, data selection and output to stdout
`bash
$ json2csv -f price
[{"price":1000},{"price":2000}]
`
Hit Enter and afterwards CTRL + D to end reading from stdin. The terminal should show
`bash`
price
1000
2000
#### Input file, data selection and output to file of multiple json files
Sometimes you want to add some additional rows with the same columns.
This is how you can do that.
`bash``Initial creation of csv with headings
$ json2csv -i test.json -f name,version > test.csvAppend additional rows
$ json2csv -i test2.json -f name,version --no-header >> test.csv
See https://juanjodiaz.github.io/json2csv/#/parsers/cli.
See LICENSE.md.