/* The MIT License (MIT) Copyright © 2020-2025 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* Single-file source-code for jsons. To compile a smaller-sized command-line app, you can use the `go` command as follows: go build -ldflags "-s -w" -trimpath jsons.go */ package main import ( "bufio" "io" "os" "strings" ) const info = ` jsons [options...] [filenames...] JSON Strings turns TSV (tab-separated values) data into a JSON array of objects whose values are strings or nulls, the latter being used for missing trailing values. ` // noMoreOutput is a custom error-type meant to be deliberately ignored type noMoreOutput struct{} func (nmo noMoreOutput) Error() string { return `no more output` } func main() { if len(os.Args) > 1 { switch os.Args[1] { case `-h`, `--h`, `-help`, `--help`: os.Stdout.WriteString(info[1:]) return } } err := run(os.Args[1:]) if _, ok := err.(noMoreOutput); ok { err = nil } if err != nil { os.Stderr.WriteString("\x1b[31m") os.Stderr.WriteString(err.Error()) os.Stderr.WriteString("\x1b[0m\n") os.Exit(1) } } type runConfig struct { lines int keys []string } func run(paths []string) error { bw := bufio.NewWriter(os.Stdout) defer bw.Flush() dashes := 0 var cfg runConfig for _, path := range paths { if path == `-` { dashes++ if dashes > 1 { continue } if err := handleInput(bw, os.Stdin, &cfg); err != nil { return err } continue } if err := handleFile(bw, path, &cfg); err != nil { return err } } if len(paths) == 0 { if err := handleInput(bw, os.Stdin, &cfg); err != nil { return err } } if cfg.lines > 1 { bw.WriteString("\n]\n") } else { bw.WriteString("[]\n") } return nil } func handleFile(w *bufio.Writer, path string, cfg *runConfig) error { f, err := os.Open(path) if err != nil { return err } defer f.Close() return handleInput(w, f, cfg) } func escapeKeys(line string) []string { var keys []string var sb strings.Builder loopTSV(line, func(i int, s string) { sb.WriteByte('"') for _, r := range s { if r == '\\' || r == '"' { sb.WriteByte('\\') } sb.WriteRune(r) } sb.WriteByte('"') keys = append(keys, sb.String()) sb.Reset() }) return keys } func emitRow(w *bufio.Writer, line string, keys []string) { j := 0 w.WriteByte('{') loopTSV(line, func(i int, s string) { j = i if i > 0 { w.WriteString(", ") } w.WriteString(keys[i]) w.WriteString(": \"") for _, r := range s { if r == '\\' || r == '"' { w.WriteByte('\\') } w.WriteRune(r) } w.WriteByte('"') }) for i := j + 1; i < len(keys); i++ { if i > 0 { w.WriteString(", ") } w.WriteString(keys[i]) w.WriteString(": null") } w.WriteByte('}') } func loopTSV(line string, f func(i int, s string)) { for i := 0; len(line) > 0; i++ { pos := strings.IndexByte(line, '\t') if pos < 0 { f(i, line) return } f(i, line[:pos]) line = line[pos+1:] } } func handleInput(w *bufio.Writer, r io.Reader, cfg *runConfig) error { const gb = 1024 * 1024 * 1024 sc := bufio.NewScanner(r) sc.Buffer(nil, 8*gb) for sc.Scan() { if cfg.lines == 0 { cfg.keys = escapeKeys(sc.Text()) w.WriteByte('[') cfg.lines++ continue } if cfg.lines == 1 { w.WriteString("\n ") } else { if _, err := w.WriteString(",\n "); err != nil { return noMoreOutput{} } } emitRow(w, sc.Text(), cfg.keys) cfg.lines++ } return sc.Err() }