Skip to content

Instantly share code, notes, and snippets.

@jrwren
Created December 10, 2019 21:39
Show Gist options
  • Save jrwren/61273074b7ba3e9388233f231416b82b to your computer and use it in GitHub Desktop.
Save jrwren/61273074b7ba3e9388233f231416b82b to your computer and use it in GitHub Desktop.
merge json files into single json with filename minus .log as key
package main
import (
"encoding/json"
"flag"
"log"
"os"
"strings"
)
func main() {
flag.Parse()
root := make(map[string]interface{})
args := flag.Args()
for i := range args {
fn := args[i]
f, err := os.Open(fn)
if err != nil {
log.Printf("error opening %s: %s", fn, err)
continue
}
if strings.HasSuffix(fn, ".log") {
fn = fn[:len(fn)-4]
}
var this map[string]interface{}
err = json.NewDecoder(f).Decode(&this)
if err != nil {
log.Printf("error decoding %s: %s", fn, err)
continue
}
root[fn] = this
f.Close()
}
err := json.NewEncoder(os.Stdout).Encode(root)
if err != nil {
log.Fatalf("error encoding json: %s", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment