MCPcopy
hub / github.com/tidwall/gjson / Get

Function Get

gjson.go:2130–2239  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

2128// If you are consuming JSON from an unpredictable source then you may want to
2129// use the Valid function first.
2130func 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

Callers 15

TestRandomValidStringsFunction · 0.85
TestEmojiFunction · 0.85
testEscapePathFunction · 0.85
TestPathFunction · 0.85
TestTimeResultFunction · 0.85
TestPlus53BitIntsFunction · 0.85
TestIssue38Function · 0.85
TestUnicodeFunction · 0.85
TestIssue6Function · 0.85
TestSingleArrayValueFunction · 0.85
TestManyBasicFunction · 0.85
testManyAnyFunction · 0.85

Calls 14

ExistsMethod · 0.95
StringMethod · 0.95
GetMethod · 0.95
execModifierFunction · 0.85
execStaticFunction · 0.85
ParseFunction · 0.85
parseSubSelectorsFunction · 0.85
ValidFunction · 0.85
AppendJSONStringFunction · 0.85
nameOfLastFunction · 0.85
isSimpleNameFunction · 0.85
parseArrayFunction · 0.85

Tested by 15

TestRandomValidStringsFunction · 0.68
TestEmojiFunction · 0.68
testEscapePathFunction · 0.68
TestPathFunction · 0.68
TestTimeResultFunction · 0.68
TestPlus53BitIntsFunction · 0.68
TestIssue38Function · 0.68
TestUnicodeFunction · 0.68
TestIssue6Function · 0.68
TestSingleArrayValueFunction · 0.68
TestManyBasicFunction · 0.68
testManyAnyFunction · 0.68