csv2json

package module
v0.0.0-...-dac99b3 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2021 License: MIT Imports: 7 Imported by: 0

README

csv2json

Donate GoDoc Go Report Card License Build Status Built with Mage

is a CSV to JSON converter inspired by the work of CSV-To-JSON-Converter.

This project was created by the intension to create json files from csv for importing the data into an ELK stack since JSON supports more options than CSV for ELK.

Features

  • convert csv to json
  • adding additional fields that are not in the original csv file
  • options PrettyPrint, QuoteEverything and LineWiseJSON

Installation

If you already installed GO on your system and configured it properly than its simply:

go get github.com/c-seeger/csv2json

If not follow these instructions.

Usage

Simple example

The following example reads a csv file defined by -c parameter and outputs to a file defined by -o

func main() {
  // read and convert csv
  f, err := os.Open("test/fixtures/data.csv")
  if err != nil {
    log.Fatal(err)
  }
  defer f.Close()

  // optional add additional fields
  addFields := make(map[string]string)
  addFields["addedField"] = "some data"

  bytes, err := csv2json.ReadCSV(f, addFields, csv2json.Options{
    PrettyPrint:     true,
    LineWiseJSON:    false,
    QuoteEverything: false,
  })
  if err != nil {
    log.Fatal(err)
  }

  fmt.Println(string(bytes))
}

Options

There are some options available in the csv2json.Options struct:

type Options struct {
        LineWiseJSON    bool
        PrettyPrint     bool
        QuoteEverything bool
}
  • LineWiseJSON
    • creates a json line per csv line and concats all lines together in one file
    • this feature is usefull if you want to generate JSON files from csv's as a data source for an ELK stack
    • if this is set to false a jSON array with every line as an entry will be created
  • PrettyPrint
    • more readable multiline version of json
  • QuoteEverything
    • ignores any kind of datatypes and quotes everything

Contribution

Thank you for participating to this project. Please see our Contribution Guidlines for more information.

Documentation

Overview

Package csv2json implementing a simple csv2 json converter

Simple example:

func main() {
	// read and convert csv
	f, err := os.Open("test/fixtures/data.csv")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	// optional add additional fields
	addFields := make(map[string]string)
	addFields["addedField"] = "some data"

	bytes, err := csv2json.ReadCSV(f, addFields, csv2json.Options{
		PrettyPrint:     true,
		LineWiseJSON:    false,
		QuoteEverything: false,
	})
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(bytes))
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadCSV

func ReadCSV(csvFile *os.File, additionalFields map[string]string, opt Options) ([]byte, error)

ReadCSV to read the content of CSV File

Types

type Options

type Options struct {
	LineWiseJSON    bool
	PrettyPrint     bool
	QuoteEverything bool
}

Options to controll the json generation

Jump to

Keyboard shortcuts

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