json.go 801 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. // +build ignore
  5. package main
  6. import (
  7. "encoding/json"
  8. "flag"
  9. "fmt"
  10. "log"
  11. "os"
  12. "strconv"
  13. "strings"
  14. )
  15. func main() {
  16. log.SetFlags(0)
  17. flag.Parse()
  18. path := strings.Split(flag.Arg(0), "/")
  19. var obj map[string]interface{}
  20. dec := json.NewDecoder(os.Stdin)
  21. dec.UseNumber()
  22. dec.Decode(&obj)
  23. var v interface{} = obj
  24. for _, p := range path {
  25. switch tv := v.(type) {
  26. case map[string]interface{}:
  27. v = tv[p]
  28. case []interface{}:
  29. i, err := strconv.Atoi(p)
  30. if err != nil {
  31. log.Fatal(err)
  32. }
  33. v = tv[i]
  34. default:
  35. return // Silence is golden
  36. }
  37. }
  38. fmt.Println(v)
  39. }