gjson: github.com/tidwall/gjson Index | Files

package gjson

import "github.com/tidwall/gjson"

Package gjson provides searching for json strings.

Index

Package Files

gjson.go

func GetMany Uses

func GetMany(json string, paths ...string) []Result

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.

func GetManyBytes Uses

func GetManyBytes(json []byte, paths ...string) []Result

GetManyBytes searches json for the specified path. If working with bytes, this method preferred over GetMany(string(data), paths...)

type Result Uses

type Result struct {
    // Type is the json type
    Type Type
    // Raw is the raw json
    Raw string
    // Str is the json string
    Str string
    // Num is the json number
    Num float64
    // Index of raw value in original json, zero means index unknown
    Index int
}

Result represents a json value that is returned from Get().

func Get Uses

func Get(json, path string) Result

Get searches json for the specified path. A path is in dot syntax, such as "name.last" or "age". 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. When the value is found it's returned immediately.

A path is a series of keys searated 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 '\'.

{
  "name": {"first": "Tom", "last": "Anderson"},
  "age":37,
  "children": ["Sara","Alex","Jack"],
  "friends": [
    {"first": "James", "last": "Murphy"},
    {"first": "Roger", "last": "Craig"}
  ]
}
"name.last"          >> "Anderson"
"age"                >> 37
"children"           >> ["Sara","Alex","Jack"]
"children.#"         >> 3
"children.1"         >> "Alex"
"child*.2"           >> "Jack"
"c?ildren.0"         >> "Sara"
"friends.#.first"    >> ["James","Roger"]

func GetBytes Uses

func GetBytes(json []byte, path string) Result

GetBytes searches json for the specified path. If working with bytes, this method preferred over Get(string(data), path)

func Parse Uses

func Parse(json string) Result

Parse parses the json and returns a result.

func ParseBytes Uses

func ParseBytes(json []byte) Result

ParseBytes parses the json and returns a result. If working with bytes, this method preferred over Parse(string(data))

func (Result) Array Uses

func (t Result) Array() []Result

Array returns back an array of values. If the result represents a non-existent value, 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.

func (Result) Bool Uses

func (t Result) Bool() bool

Bool returns an boolean representation.

func (Result) Exists Uses

func (t Result) Exists() bool

Exists returns true if value exists.

 if gjson.Get(json, "name.last").Exists(){
		println("value exists")
 }

func (Result) Float Uses

func (t Result) Float() float64

Float returns an float64 representation.

func (Result) ForEach Uses

func (t Result) ForEach(iterator func(key, value Result) bool)

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.

func (Result) Get Uses

func (t Result) Get(path string) Result

Get searches result for the specified path. The result should be a JSON array or object.

func (Result) Int Uses

func (t Result) Int() int64

Int returns an integer representation.

func (Result) Less Uses

func (t Result) Less(token Result, caseSensitive bool) bool

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:

Null < False < Number < String < True < JSON

func (Result) Map Uses

func (t Result) Map() map[string]Result

Map returns back an map of values. The result should be a JSON array.

func (Result) String Uses

func (t Result) String() string

String returns a string representation of the value.

func (Result) Uint Uses

func (t Result) Uint() uint64

Uint returns an unsigned integer representation.

func (Result) Value Uses

func (t Result) Value() interface{}

Value returns one of these types:

bool, for JSON booleans
float64, for JSON numbers
Number, for JSON numbers
string, for JSON string literals
nil, for JSON null

type Type Uses

type Type int

Type is Result type

const (
    // Null is a null json value
    Null Type = iota
    // False is a json false boolean
    False
    // Number is json number
    Number
    // String is a json string
    String
    // True is a json true boolean
    True
    // JSON is a raw block of JSON
    JSON
)

func (Type) String Uses

func (t Type) String() string

String returns a string representation of the type.

Package gjson imports 4 packages (graph) and is imported by 25 packages. Updated 2017-03-29. Refresh now. Tools for package owners.