Utility package for converting CSV++ data to JSON and YAML formats.
This package uses encoding/json/jsontext from Go's experimental JSON v2 implementation.
GOEXPERIMENT=jsonv2 go build ./...
GOEXPERIMENT=jsonv2 go test ./...- Streaming JSON output - Memory-efficient for large files
- YAML output - With preserved key order
- Full CSV++ field type support - SimpleField, ArrayField, StructuredField, ArrayStructuredField
For large datasets, use streaming writers to minimize memory usage.
w := csvpputil.NewJSONArrayWriter(os.Stdout, headers)
for _, record := range records {
if err := w.Write(record); err != nil {
return err
}
}
if err := w.Close(); err != nil {
return err
}w := csvpputil.NewYAMLArrayWriter(os.Stdout, headers,
csvpputil.WithYAMLCapacity(1000), // optional: pre-allocate buffer
)
for _, record := range records {
if err := w.Write(record); err != nil {
return err
}
}
if err := w.Close(); err != nil {
return err
}Options:
WithYAMLCapacity(n)- Pre-allocates the internal buffer fornrecords, reducing memory allocations when the approximate record count is known.
Note: YAML output is buffered until Close() due to go-yaml library constraints.
For small to medium datasets, use these one-shot functions.
// CSV++ to JSON bytes
jsonBytes, err := csvpputil.MarshalJSON(headers, records)
// CSV++ to YAML bytes
yamlBytes, err := csvpputil.MarshalYAML(headers, records)// Write JSON to io.Writer
err := csvpputil.WriteJSON(w, headers, records)
// Write YAML to io.Writer
err := csvpputil.WriteYAML(w, headers, records)package main
import (
"fmt"
"log"
"github.com/osamingo/go-csvpp"
"github.com/osamingo/go-csvpp/csvpputil"
)
func main() {
headers := []*csvpp.ColumnHeader{
{Name: "name", Kind: csvpp.SimpleField},
{Name: "age", Kind: csvpp.SimpleField},
}
records := [][]*csvpp.Field{
{{Value: "Alice"}, {Value: "30"}},
{{Value: "Bob"}, {Value: "25"}},
}
// Convert to JSON
data, err := csvpputil.MarshalJSON(headers, records)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))
// Output: [{"name":"Alice","age":"30"},{"name":"Bob","age":"25"}]
// Convert to YAML
yamlData, err := csvpputil.MarshalYAML(headers, records)
if err != nil {
log.Fatal(err)
}
fmt.Print(string(yamlData))
// Output:
// - name: Alice
// age: "30"
// - name: Bob
// age: "25"
}| CSV++ Field Type | JSON Output | YAML Output |
|---|---|---|
| SimpleField | "value" |
value |
| ArrayField | ["a", "b"] |
- a- b |
| StructuredField | {"k1": "v1", "k2": "v2"} |
k1: v1k2: v2 |
| ArrayStructuredField | [{"k": "v"}, ...] |
- k: v- ... |
See the LICENSE file in the repository root.