1package json
2
3import (
4	"strconv"
5	"strings"
6)
7
8// Get the value at the specified path.
9func Get(m map[string]interface{}, path string) interface{} {
10	if path == "" {
11		return m
12	}
13
14	parts := strings.Split(path[1:], "/")
15	var rv interface{} = m
16
17	for _, p := range parts {
18		switch v := rv.(type) {
19		case map[string]interface{}:
20			if strings.Contains(p, "~") {
21				p = strings.Replace(p, "~1", "/", -1)
22				p = strings.Replace(p, "~0", "~", -1)
23			}
24			rv = v[p]
25		case []interface{}:
26			i, err := strconv.Atoi(p)
27			if err == nil && i < len(v) {
28				rv = v[i]
29			} else {
30				return nil
31			}
32		default:
33			return nil
34		}
35	}
36
37	return rv
38}
39