Advertisement
florianwalther

beautyfyJSON in go

Jul 13th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.65 KB | None | 0 0
  1. //Build with: go build ./jsonpp.go
  2.  
  3. package main
  4.  
  5. import "encoding/json"
  6. import "fmt"
  7. import "io/ioutil"
  8. import "os"
  9.  
  10.  
  11. func main() {
  12.     if len(os.Args) != 2 {
  13.        fmt.Println("One argument, the json file to pretty-print is required")
  14.        os.Exit(-1)
  15.     }
  16.  
  17.     fileName := os.Args[1]
  18.     byt, err := ioutil.ReadFile(fileName)
  19.     if err != nil {
  20.        panic(err)
  21.     }
  22.  
  23.     var dat map[string] interface{}
  24.  
  25.     if err := json.Unmarshal(byt, &dat); err != nil {
  26.         panic(err)
  27.     }
  28.     b, err := json.MarshalIndent(dat, "", "  ")
  29.     if err != nil {
  30.        panic(err)
  31.     }
  32.     b2 := append(b, '\n')
  33.     os.Stdout.Write(b2)
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement