Get searches json for the specified path. A path is in dot syntax, such as "name.last" or "age". When the value is found it's returned immediately. A path is a series of keys separated by a dot. A key may contain special wildcard characters '*' and '?'. To access an array value use the index as the
(json, path string)
| 2128 | // If you are consuming JSON from an unpredictable source then you may want to |
| 2129 | // use the Valid function first. |
| 2130 | func Get(json, path string) Result { |
| 2131 | if len(path) > 1 { |
| 2132 | if (path[0] == '@' && !DisableModifiers) || path[0] == '!' { |
| 2133 | // possible modifier |
| 2134 | var ok bool |
| 2135 | var npath string |
| 2136 | var rjson string |
| 2137 | if path[0] == '@' && !DisableModifiers { |
| 2138 | npath, rjson, ok = execModifier(json, path) |
| 2139 | } else if path[0] == '!' { |
| 2140 | npath, rjson, ok = execStatic(json, path) |
| 2141 | } |
| 2142 | if ok { |
| 2143 | path = npath |
| 2144 | if len(path) > 0 && (path[0] == '|' || path[0] == '.') { |
| 2145 | res := Get(rjson, path[1:]) |
| 2146 | res.Index = 0 |
| 2147 | res.Indexes = nil |
| 2148 | return res |
| 2149 | } |
| 2150 | return Parse(rjson) |
| 2151 | } |
| 2152 | } |
| 2153 | if path[0] == '[' || path[0] == '{' { |
| 2154 | // using a subselector path |
| 2155 | kind := path[0] |
| 2156 | var ok bool |
| 2157 | var subs []subSelector |
| 2158 | subs, path, ok = parseSubSelectors(path) |
| 2159 | if ok { |
| 2160 | if len(path) == 0 || (path[0] == '|' || path[0] == '.') { |
| 2161 | var b []byte |
| 2162 | b = append(b, kind) |
| 2163 | var i int |
| 2164 | for _, sub := range subs { |
| 2165 | res := Get(json, sub.path) |
| 2166 | if res.Exists() { |
| 2167 | if i > 0 { |
| 2168 | b = append(b, ',') |
| 2169 | } |
| 2170 | if kind == '{' { |
| 2171 | if len(sub.name) > 0 { |
| 2172 | if sub.name[0] == '"' && Valid(sub.name) { |
| 2173 | b = append(b, sub.name...) |
| 2174 | } else { |
| 2175 | b = AppendJSONString(b, sub.name) |
| 2176 | } |
| 2177 | } else { |
| 2178 | last := nameOfLast(sub.path) |
| 2179 | if isSimpleName(last) { |
| 2180 | b = AppendJSONString(b, last) |
| 2181 | } else { |
| 2182 | b = AppendJSONString(b, "_") |
| 2183 | } |
| 2184 | } |
| 2185 | b = append(b, ':') |
| 2186 | } |
| 2187 | var raw string |