A command-line utility for converting CSV data to JSON format with advanced configuration options.
The csv2json utility transforms CSV data into JSON, YAML, or TOML formats with flexible mapping options. It allows you to:
go install github.com/sascha-andres/jsonedit/cmd/csv2json@latest
Download the appropriate binary for your platform from the releases page.
csv2json --configuration-file=config.json --input-file=data.csv --output-file=result.json
Convert a CSV file to JSON:
csv2json --configuration-file=config.json --input-file=input.csv --output-file=output.json
Read from stdin and write to stdout:
cat input.csv | csv2json --configuration-file=config.json > output.json
Option | Default | Description |
---|---|---|
--separator |
; |
Separator character used in the CSV input |
--output-type |
json |
Output format type (json , yaml , or toml ) |
--configuration-file |
(required) | Path to the configuration file |
--nested-property-name |
(empty) | Name of the property to use for nested arrays (for TOML output) |
--generate-array |
false |
Generate an array for the output instead of individual objects |
--access-by-header |
false |
Access CSV columns by header name instead of by index |
--input-file |
- (stdin) |
Path to the input file (- for stdin) |
--output-file |
- (stdout) |
Path to the output file (- for stdout) |
--debug |
false |
Enable debug mode with detailed logging to stdout |
The configuration file defines how CSV columns are mapped to the output format. It supports JSON, YAML, or TOML formats.
{
"mapping": {
"0": {
"property": "id",
"type": "int"
},
"1": {
"property": "name",
"type": "string"
},
"2": {
"properties": [
{
"property": "address.street",
"type": "string"
},
{
"property": "address.city",
"type": "string"
}
]
}
},
"calculated": [
{
"property": "timestamp",
"kind": "datetime",
"format": "2006-01-02T15:04:05Z07:00",
"type": "string",
"location": "document"
}
],
"extra_variables": {
"version": {
"value": "1.0"
}
}
}
The mapping
section maps CSV columns to output properties:
--access-by-header=false
) or header names (when --access-by-header=true
)property
and type
for direct mappingproperties
for mapping one column to multiple output fieldsProperties can be conditionally included in the output based on specified criteria:
condition
object to a property mapping to make it conditionalstring
- Text valuesint
- Integer valuesfloat
- Floating-point valuesbool
- Boolean values (true/false)The calculated
section defines dynamic fields:
property
: The name of the property in the outputkind
: The type of calculation (datetime
or application
)format
: Format string (for datetime) or value to acquiretype
: Output data typelocation
: Where to apply the field (document
or record
)The extra_variables
section defines static variables to include in the output.
For a CSV file users.csv
:
1;John Doe;john@example.com
2;Jane Smith;jane@example.com
With configuration config.json
:
{
"mapping": {
"0": {
"property": "id",
"type": "int"
},
"1": {
"property": "name",
"type": "string"
},
"2": {
"property": "email",
"type": "string"
}
}
}
Command:
csv2json --configuration-file=config.json --input-file=users.csv --output-file=users.json
Output (in JSON Lines format, with each line being a valid JSON object):
{"id":1,"name":"John Doe","email":"john@example.com"}
{"id":2,"name":"Jane Smith","email":"jane@example.com"}
Using the same example but with the --generate-array
flag:
csv2json --configuration-file=config.json --input-file=users.csv --output-file=users.json --generate-array
Output:
[
{"id":1,"name":"John Doe","email":"john@example.com"},
{"id":2,"name":"Jane Smith","email":"jane@example.com"}
]
The utility will exit with an error message if: