json.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // +build ignore
  16. package main
  17. import (
  18. "encoding/json"
  19. "flag"
  20. "fmt"
  21. "log"
  22. "os"
  23. "strconv"
  24. "strings"
  25. )
  26. func main() {
  27. log.SetFlags(0)
  28. flag.Parse()
  29. path := strings.Split(flag.Arg(0), "/")
  30. var obj map[string]interface{}
  31. dec := json.NewDecoder(os.Stdin)
  32. dec.UseNumber()
  33. dec.Decode(&obj)
  34. var v interface{} = obj
  35. for _, p := range path {
  36. switch tv := v.(type) {
  37. case map[string]interface{}:
  38. v = tv[p]
  39. case []interface{}:
  40. i, err := strconv.Atoi(p)
  41. if err != nil {
  42. log.Fatal(err)
  43. }
  44. v = tv[i]
  45. default:
  46. return // Silence is golden
  47. }
  48. }
  49. fmt.Println(v)
  50. }