...

Source file src/github.com/PaesslerAG/jsonpath/jsonpath.go

Documentation: github.com/PaesslerAG/jsonpath

     1  // Package jsonpath is an implementation of http://goessner.net/articles/JsonPath/
     2  // If a JSONPath contains one of
     3  // [key1, key2 ...], .., *, [min:max], [min:max:step], (? expression)
     4  // all matchs are listed in an []interface{}
     5  //
     6  // The package comes with an extension of JSONPath to access the wildcard values of a match.
     7  // If the JSONPath is used inside of a JSON object, you can use placeholder '#' or '#i' with natural number i
     8  // to access all wildcards values or the ith wildcard
     9  //
    10  // This package can be extended with gval modules for script features like multiply, length, regex or many more.
    11  // So take a look at github.com/PaesslerAG/gval.
    12  package jsonpath
    13  
    14  import (
    15  	"context"
    16  
    17  	"github.com/PaesslerAG/gval"
    18  )
    19  
    20  // New returns an selector for given JSONPath
    21  func New(path string) (gval.Evaluable, error) {
    22  	return lang.NewEvaluable(path)
    23  }
    24  
    25  //Get executes given JSONPath on given value
    26  func Get(path string, value interface{}) (interface{}, error) {
    27  	eval, err := lang.NewEvaluable(path)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	return eval(context.Background(), value)
    32  }
    33  
    34  var lang = gval.NewLanguage(
    35  	gval.Base(),
    36  	gval.PrefixExtension('$', parseRootPath),
    37  	gval.PrefixExtension('@', parseCurrentPath),
    38  )
    39  
    40  //Language is the JSONPath Language
    41  func Language() gval.Language {
    42  	return lang
    43  }
    44  
    45  var placeholderExtension = gval.NewLanguage(
    46  	lang,
    47  	gval.PrefixExtension('{', parseJSONObject),
    48  	gval.PrefixExtension('#', parsePlaceholder),
    49  )
    50  
    51  //PlaceholderExtension is the JSONPath Language with placeholder
    52  func PlaceholderExtension() gval.Language {
    53  	return placeholderExtension
    54  }
    55  

View as plain text