json2csv

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2019 License: MIT Imports: 10 Imported by: 0

README

Build Status

json2csv

Convert JSON into CSV. CSV header is generated from each key path as a JSON Pointer. json2csv can be used as a library and command line tool.

Install

Use go get or just download binary releases.

go get github.com/yukithm/json2csv/cmd/json2csv

Usage

json2csv reads JSON content from STDIN or the file specified by the argument.

json2csv example.json
cat example.json | json2csv

Convert object array:

[
    {
        "id": 1,
        "name": "foo",
        "favorites": {
            "fruits": "apple",
            "color": "red"
        }
    },
    {
        "id": 2,
        "name": "bar",
        "favorites": {
            "fruits": "orange"
        }
    },
    {
        "id": 3,
        "name": "baz",
        "favorites": {
            "fruits": "banana",
            "color": "yellow"
        }
    }
]
$ json2csv example1.json

/id,/name,/favorites/color,/favorites/fruits
1,foo,red,apple
2,bar,,orange
3,baz,yellow,banana

Convert object:

{
    "status": 200,
    "result": [
        {
            "id": 1,
            "name": "foo"
        },
        {
            "id": 2,
            "name": "bar"
        }
    ]
}
$ json2csv example2.json

/status,/result/0/id,/result/0/name,/result/1/id,/result/1/name
200,1,foo,2,bar

Convert an array of the object:

Use --path=<JSON Pointer> option.

$ json2csv --path=/result example2.json

/id,/name
1,foo
2,bar

Transpose rows and columns:

$ json2csv --transpose example1.json

/id,1,2,3
/name,foo,bar,baz
/favorites/color,red,,yellow
/favorites/fruits,apple,orange,banana
Header styles

By default, header is represented with JSON Pointer. --header-style=STYLE option can change styles.

style example
jsonpointer /foo/bar/0/baz
slash foo/bar/0/baz
dot foo.bar.0.baz
dot-bracket foo.bar[0].baz

Note: slash style similar to jsonpointer style, but slash style doesn't start with '/' and doesn't escape special characters ('/' and '~') defined in RFC 6901.

Note: dot-bracket style similar to dot style, but dot-bracket style uses square brackets for array indexes.

License

MIT

Author

Yuki (@yukithm)

Documentation

Overview

Package json2csv provides JSON to CSV functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CSVWriter

type CSVWriter struct {
	*csv.Writer
	HeaderStyle KeyStyle
	Transpose   bool
}

CSVWriter writes CSV data.

func NewCSVWriter

func NewCSVWriter(w io.Writer) *CSVWriter

NewCSVWriter returns new CSVWriter with JSONPointerStyle.

func (*CSVWriter) WriteCSV

func (w *CSVWriter) WriteCSV(results []KeyValue) error

WriteCSV writes CSV data.

func (*CSVWriter) WriteCSVWithoutHeader added in v0.1.2

func (w *CSVWriter) WriteCSVWithoutHeader(results []KeyValue, ignoreHeader bool) error

WriteCSV writes CSV data without header

type KeyStyle

type KeyStyle uint

KeyStyle represents the specific style of the key.

const (
	// "/foo/bar/0/baz"
	JSONPointerStyle KeyStyle = iota

	// "foo/bar/0/baz"
	SlashStyle

	// "foo.bar.0.baz"
	DotNotationStyle

	// "foo.bar[0].baz"
	DotBracketStyle
)

Header style

type KeyValue

type KeyValue map[string]interface{}

KeyValue represents key(path)/value map.

func JSON2CSV

func JSON2CSV(data interface{}) ([]KeyValue, error)

JSON2CSV converts JSON to CSV.

func (KeyValue) Keys

func (kv KeyValue) Keys() []string

Keys returns all keys.

Directories

Path Synopsis
cmd
Package jsonpointer implements representations for JSON Pointer and tokens.
Package jsonpointer implements representations for JSON Pointer and tokens.

Jump to

Keyboard shortcuts

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