jsonedit

csv2json

A command-line utility for converting CSV data to JSON format with advanced configuration options.

Overview

The csv2json utility transforms CSV data into JSON, YAML, or TOML formats with flexible mapping options. It allows you to:

Installation

From Source

go install github.com/sascha-andres/jsonedit/cmd/csv2json@latest

From Binary Releases

Download the appropriate binary for your platform from the releases page.

Usage

csv2json --configuration-file=config.json --input-file=data.csv --output-file=result.json

Basic Example

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

Command-Line Options

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

Configuration File Format

The configuration file defines how CSV columns are mapped to the output format. It supports JSON, YAML, or TOML formats.

Example Configuration

{
  "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"
    }
  }
}

Configuration Structure

Mapping

The mapping section maps CSV columns to output properties:

Conditional Mapping

Properties can be conditionally included in the output based on specified criteria:

Supported Types

Calculated Fields

The calculated section defines dynamic fields:

Extra Variables

The extra_variables section defines static variables to include in the output.

Examples

Simple CSV to JSON Conversion

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"}

Generating an Array

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"}
]

Error Handling

The utility will exit with an error message if: