csv2json

command module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2023 License: MIT Imports: 7 Imported by: 0

README

csv2json

Overview

csv2json is a simple command line interface (CLI) tool that converts CSV files to JSON format.

Installation

csv2json can be installed using the go install command.

$ go install github.com/fujiwara/csv2json@latest

How to Use

csv2json can be used either by specifying a CSV file as an argument or by piping data in through standard input.

$ csv2json file.csv
$ cat file.csv | csv2json

The tool reads the CSV data, parses it, and then outputs the data in JSON format to standard output.

Flags

-n

The -n flag specifies that the CSV file does not have a header row. If this flag is set, each row is output as a simple JSON array.

$ csv2json -n file.csv

Example output:

[
  "1",
  "John",
  "Doe"
]
[
  "2",
  "Jane",
  "Foo"
]

If the -n flag is not set and the CSV file contains a header row, the tool uses the header row to create JSON objects. The keys in the JSON objects correspond to the fields in the header row, and the values correspond to the data in each record.

$ csv2json file.csv

Example output.

{
  "id": "1",
  "first_name": "John",
  "last_name": "Doe"
}
{
  "id": "2",
  "first_name": "Jane",
  "last_name": "Foo"
}
-c

The -c flag specifies that the JSON output should be in a compact format. If this flag is set, the JSON objects will be output without any extra white space.

Error Handling

csv2json handles different types of errors that can occur during CSV parsing.

If the tool encounters a record with a different number of fields than expected (as determined by the first record), it will print an error message and continue with the next record. It also handles and reports other types of errors while reading the CSV data.

JSON Output

The JSON output is formatted with an indent of two spaces for better readability.

Note

Be aware that csv2json will panic and exit immediately if it encounters an error while encoding to JSON. In such a case, check your CSV data for any irregularities.

License

MIT License

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL