Files
gists/projects/go-tools/csv2json/csv2json.go
T
tobias 1cbf8afb4a chore: cleanup — untrack binaries, consolidate Go dirs, dedupe tools
- Untrack and delete compiled binaries (tarsum, gosoft.exe, rust uniq/uniq2);
  ignore build outputs (dist/, bin/, *.exe, *.test, .ruff_cache/)
- Merge tools/go/ and projects/go-tools/go/ into projects/go-tools/<name>/
- Fix goipgrep .gitignore: bare 'ipgrep' pattern was ignoring cmd/ipgrep/,
  so the main entrypoint was never tracked; now anchored to /ipgrep
- Archive duplicate implementations to archive/experimental/{rust,go}/
  (uniq, between, tarsum rewrites); canonical versions stay in tools/
- Update README tool catalog to match new layout

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 13:42:45 +02:00

57 lines
1.1 KiB
Go

package main
import (
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"log"
"os"
)
func csvToJson(inputSource *os.File) {
csvReader := csv.NewReader(inputSource)
headers, err := csvReader.Read()
if err != nil {
log.Fatal("Failed to read headers: ", err)
}
for {
record, err := csvReader.Read()
if err != nil {
if err.Error() == "EOF" {
break
}
log.Fatal("Failed to read the data: ", err)
}
if len(record) == len(headers) {
jsonData := make(map[string]string)
for i, value := range record {
jsonData[headers[i]] = value
}
jsonOutput, err := json.Marshal(jsonData)
if err != nil {
log.Fatal("Failed to convert to json: ", err)
}
fmt.Println(string(jsonOutput))
} else {
log.Fatal("The number of columns in the record is not equal to the number of headers")
}
}
}
func main() {
inputSource := os.Stdin
flag.Parse()
var err error
if flag.NArg() > 0 {
inputSource, err = os.Open(flag.Args()[0])
if err != nil {
log.Fatalf("Failed to open file: %v\n", err)
}
defer inputSource.Close()
}
csvToJson(inputSource)
}