Created
December 10, 2019 21:39
-
-
Save jrwren/61273074b7ba3e9388233f231416b82b to your computer and use it in GitHub Desktop.
merge json files into single json with filename minus .log as key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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