github.com/fufuok/utils@v1.0.10/xjson/gjson/DOC.md (about) 1 <!-- Code generated by gomarkdoc. DO NOT EDIT --> 2 3 # gjson 4 5 ```go 6 import "github.com/fufuok/utils/xjson/gjson" 7 ``` 8 9 Package gjson provides searching for json strings. 10 11 ## Index 12 13 - [Variables](<#variables>) 14 - [func AddModifier(name string, fn func(json, arg string) string)](<#func-addmodifier>) 15 - [func AppendJSONString(dst []byte, s string) []byte](<#func-appendjsonstring>) 16 - [func Escape(comp string) string](<#func-escape>) 17 - [func ForEachLine(json string, iterator func(line Result) bool)](<#func-foreachline>) 18 - [func ModifierExists(name string, fn func(json, arg string) string) bool](<#func-modifierexists>) 19 - [func Valid(json string) bool](<#func-valid>) 20 - [func ValidBytes(json []byte) bool](<#func-validbytes>) 21 - [type Result](<#type-result>) 22 - [func Get(json, path string) Result](<#func-get>) 23 - [func GetBytes(json []byte, path string) Result](<#func-getbytes>) 24 - [func GetMany(json string, path ...string) []Result](<#func-getmany>) 25 - [func GetManyBytes(json []byte, path ...string) []Result](<#func-getmanybytes>) 26 - [func Parse(json string) Result](<#func-parse>) 27 - [func ParseBytes(json []byte) Result](<#func-parsebytes>) 28 - [func (t Result) Array() []Result](<#func-result-array>) 29 - [func (t Result) Bool() bool](<#func-result-bool>) 30 - [func (t Result) Exists() bool](<#func-result-exists>) 31 - [func (t Result) Float() float64](<#func-result-float>) 32 - [func (t Result) ForEach(iterator func(key, value Result) bool)](<#func-result-foreach>) 33 - [func (t Result) Get(path string) Result](<#func-result-get>) 34 - [func (t Result) Int() int64](<#func-result-int>) 35 - [func (t Result) IsArray() bool](<#func-result-isarray>) 36 - [func (t Result) IsBool() bool](<#func-result-isbool>) 37 - [func (t Result) IsObject() bool](<#func-result-isobject>) 38 - [func (t Result) Less(token Result, caseSensitive bool) bool](<#func-result-less>) 39 - [func (t Result) Map() map[string]Result](<#func-result-map>) 40 - [func (t Result) Path(json string) string](<#func-result-path>) 41 - [func (t Result) Paths(json string) []string](<#func-result-paths>) 42 - [func (t Result) String() string](<#func-result-string>) 43 - [func (t Result) Time() time.Time](<#func-result-time>) 44 - [func (t Result) Uint() uint64](<#func-result-uint>) 45 - [func (t Result) Value() interface{}](<#func-result-value>) 46 - [type Type](<#type-type>) 47 - [func (t Type) String() string](<#func-type-string>) 48 49 50 ## Variables 51 52 DisableModifiers will disable the modifier syntax 53 54 ```go 55 var DisableModifiers = false 56 ``` 57 58 ## func AddModifier 59 60 ```go 61 func AddModifier(name string, fn func(json, arg string) string) 62 ``` 63 64 AddModifier binds a custom modifier command to the GJSON syntax. This operation is not thread safe and should be executed prior to using all other gjson function. 65 66 ## func AppendJSONString 67 68 ```go 69 func AppendJSONString(dst []byte, s string) []byte 70 ``` 71 72 AppendJSONString is a convenience function that converts the provided string to a valid JSON string and appends it to dst. 73 74 ## func Escape 75 76 ```go 77 func Escape(comp string) string 78 ``` 79 80 Escape returns an escaped path component. 81 82 ``` 83 json := `{ 84 "user":{ 85 "first.name": "Janet", 86 "last.name": "Prichard" 87 } 88 }` 89 user := gjson.Get(json, "user") 90 println(user.Get(gjson.Escape("first.name")) 91 println(user.Get(gjson.Escape("last.name")) 92 // Output: 93 // Janet 94 // Prichard 95 ``` 96 97 ## func ForEachLine 98 99 ```go 100 func ForEachLine(json string, iterator func(line Result) bool) 101 ``` 102 103 ForEachLine iterates through lines of JSON as specified by the JSON Lines format \(http://jsonlines.org/\). Each line is returned as a GJSON Result. 104 105 ## func ModifierExists 106 107 ```go 108 func ModifierExists(name string, fn func(json, arg string) string) bool 109 ``` 110 111 ModifierExists returns true when the specified modifier exists. 112 113 ## func Valid 114 115 ```go 116 func Valid(json string) bool 117 ``` 118 119 Valid returns true if the input is valid json. 120 121 ``` 122 if !gjson.Valid(json) { 123 return errors.New("invalid json") 124 } 125 value := gjson.Get(json, "name.last") 126 ``` 127 128 ## func ValidBytes 129 130 ```go 131 func ValidBytes(json []byte) bool 132 ``` 133 134 ValidBytes returns true if the input is valid json. 135 136 ``` 137 if !gjson.Valid(json) { 138 return errors.New("invalid json") 139 } 140 value := gjson.Get(json, "name.last") 141 ``` 142 143 If working with bytes, this method preferred over ValidBytes\(string\(data\)\) 144 145 ## type Result 146 147 Result represents a json value that is returned from Get\(\). 148 149 ```go 150 type Result struct { 151 // Type is the json type 152 Type Type 153 // Raw is the raw json 154 Raw string 155 // Str is the json string 156 Str string 157 // Num is the json number 158 Num float64 159 // Index of raw value in original json, zero means index unknown 160 Index int 161 // Indexes of all the elements that match on a path containing the '#' 162 // query character. 163 Indexes []int 164 } 165 ``` 166 167 ### func Get 168 169 ```go 170 func Get(json, path string) Result 171 ``` 172 173 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. 174 175 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 key. To get the number of elements in an array or to access a child path, use the '\#' character. The dot and wildcard character can be escaped with '\\'. 176 177 ``` 178 { 179 "name": {"first": "Tom", "last": "Anderson"}, 180 "age":37, 181 "children": ["Sara","Alex","Jack"], 182 "friends": [ 183 {"first": "James", "last": "Murphy"}, 184 {"first": "Roger", "last": "Craig"} 185 ] 186 } 187 "name.last" >> "Anderson" 188 "age" >> 37 189 "children" >> ["Sara","Alex","Jack"] 190 "children.#" >> 3 191 "children.1" >> "Alex" 192 "child*.2" >> "Jack" 193 "c?ildren.0" >> "Sara" 194 "friends.#.first" >> ["James","Roger"] 195 ``` 196 197 This function expects that the json is well\-formed, and does not validate. Invalid json will not panic, but it may return back unexpected results. If you are consuming JSON from an unpredictable source then you may want to use the Valid function first. 198 199 ### func GetBytes 200 201 ```go 202 func GetBytes(json []byte, path string) Result 203 ``` 204 205 GetBytes searches json for the specified path. If working with bytes, this method preferred over Get\(string\(data\), path\) 206 207 ### func GetMany 208 209 ```go 210 func GetMany(json string, path ...string) []Result 211 ``` 212 213 GetMany searches json for the multiple paths. The return value is a Result array where the number of items will be equal to the number of input paths. 214 215 ### func GetManyBytes 216 217 ```go 218 func GetManyBytes(json []byte, path ...string) []Result 219 ``` 220 221 GetManyBytes searches json for the multiple paths. The return value is a Result array where the number of items will be equal to the number of input paths. 222 223 ### func Parse 224 225 ```go 226 func Parse(json string) Result 227 ``` 228 229 Parse parses the json and returns a result. 230 231 This function expects that the json is well\-formed, and does not validate. Invalid json will not panic, but it may return back unexpected results. If you are consuming JSON from an unpredictable source then you may want to use the Valid function first. 232 233 ### func ParseBytes 234 235 ```go 236 func ParseBytes(json []byte) Result 237 ``` 238 239 ParseBytes parses the json and returns a result. If working with bytes, this method preferred over Parse\(string\(data\)\) 240 241 ### func \(Result\) Array 242 243 ```go 244 func (t Result) Array() []Result 245 ``` 246 247 Array returns back an array of values. If the result represents a null value or is non\-existent, then an empty array will be returned. If the result is not a JSON array, the return value will be an array containing one result. 248 249 ### func \(Result\) Bool 250 251 ```go 252 func (t Result) Bool() bool 253 ``` 254 255 Bool returns an boolean representation. 256 257 ### func \(Result\) Exists 258 259 ```go 260 func (t Result) Exists() bool 261 ``` 262 263 Exists returns true if value exists. 264 265 ``` 266 if gjson.Get(json, "name.last").Exists(){ 267 println("value exists") 268 } 269 ``` 270 271 ### func \(Result\) Float 272 273 ```go 274 func (t Result) Float() float64 275 ``` 276 277 Float returns an float64 representation. 278 279 ### func \(Result\) ForEach 280 281 ```go 282 func (t Result) ForEach(iterator func(key, value Result) bool) 283 ``` 284 285 ForEach iterates through values. If the result represents a non\-existent value, then no values will be iterated. If the result is an Object, the iterator will pass the key and value of each item. If the result is an Array, the iterator will only pass the value of each item. If the result is not a JSON array or object, the iterator will pass back one value equal to the result. 286 287 ### func \(Result\) Get 288 289 ```go 290 func (t Result) Get(path string) Result 291 ``` 292 293 Get searches result for the specified path. The result should be a JSON array or object. 294 295 ### func \(Result\) Int 296 297 ```go 298 func (t Result) Int() int64 299 ``` 300 301 Int returns an integer representation. 302 303 ### func \(Result\) IsArray 304 305 ```go 306 func (t Result) IsArray() bool 307 ``` 308 309 IsArray returns true if the result value is a JSON array. 310 311 ### func \(Result\) IsBool 312 313 ```go 314 func (t Result) IsBool() bool 315 ``` 316 317 IsBool returns true if the result value is a JSON boolean. 318 319 ### func \(Result\) IsObject 320 321 ```go 322 func (t Result) IsObject() bool 323 ``` 324 325 IsObject returns true if the result value is a JSON object. 326 327 ### func \(Result\) Less 328 329 ```go 330 func (t Result) Less(token Result, caseSensitive bool) bool 331 ``` 332 333 Less return true if a token is less than another token. The caseSensitive paramater is used when the tokens are Strings. The order when comparing two different type is: 334 335 ``` 336 Null < False < Number < String < True < JSON 337 ``` 338 339 ### func \(Result\) Map 340 341 ```go 342 func (t Result) Map() map[string]Result 343 ``` 344 345 Map returns back a map of values. The result should be a JSON object. If the result is not a JSON object, the return value will be an empty map. 346 347 ### func \(Result\) Path 348 349 ```go 350 func (t Result) Path(json string) string 351 ``` 352 353 Path returns the original GJSON path for a Result where the Result came from a simple path that returns a single value, like: 354 355 ``` 356 gjson.Get(json, "friends.#(last=Murphy)") 357 ``` 358 359 The returned value will be in the form of a JSON string: 360 361 ``` 362 "friends.0" 363 ``` 364 365 The param 'json' must be the original JSON used when calling Get. 366 367 Returns an empty string if the paths cannot be determined, which can happen when the Result came from a path that contained a multipath, modifier, or a nested query. 368 369 ### func \(Result\) Paths 370 371 ```go 372 func (t Result) Paths(json string) []string 373 ``` 374 375 Paths returns the original GJSON paths for a Result where the Result came from a simple query path that returns an array, like: 376 377 ``` 378 gjson.Get(json, "friends.#.first") 379 ``` 380 381 The returned value will be in the form of a JSON array: 382 383 ``` 384 ["friends.0.first","friends.1.first","friends.2.first"] 385 ``` 386 387 The param 'json' must be the original JSON used when calling Get. 388 389 Returns an empty string if the paths cannot be determined, which can happen when the Result came from a path that contained a multipath, modifier, or a nested query. 390 391 ### func \(Result\) String 392 393 ```go 394 func (t Result) String() string 395 ``` 396 397 String returns a string representation of the value. 398 399 ### func \(Result\) Time 400 401 ```go 402 func (t Result) Time() time.Time 403 ``` 404 405 Time returns a time.Time representation. 406 407 ### func \(Result\) Uint 408 409 ```go 410 func (t Result) Uint() uint64 411 ``` 412 413 Uint returns an unsigned integer representation. 414 415 ### func \(Result\) Value 416 417 ```go 418 func (t Result) Value() interface{} 419 ``` 420 421 Value returns one of these types: 422 423 ``` 424 bool, for JSON booleans 425 float64, for JSON numbers 426 Number, for JSON numbers 427 string, for JSON string literals 428 nil, for JSON null 429 map[string]interface{}, for JSON objects 430 []interface{}, for JSON arrays 431 ``` 432 433 ## type Type 434 435 Type is Result type 436 437 ```go 438 type Type int 439 ``` 440 441 ```go 442 const ( 443 // Null is a null json value 444 Null Type = iota 445 // False is a json false boolean 446 False 447 // Number is json number 448 Number 449 // String is a json string 450 String 451 // True is a json true boolean 452 True 453 // JSON is a raw block of JSON 454 JSON 455 ) 456 ``` 457 458 ### func \(Type\) String 459 460 ```go 461 func (t Type) String() string 462 ``` 463 464 String returns a string representation of the type. 465 466 467 468 Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)