Documentation ¶
Overview ¶
Package jp provides JSONPath implementation that operations on simple go types, generic (gen package), and public struct with public members. Get, set, and delete operations can be evaluated on data. When needed reflection is used to follow a path.
Index ¶
- Variables
- func AppendString(buf []byte, s string, delim byte) []byte
- func Walk(data any, cb func(path Expr, value any), justLeaves ...bool)
- type At
- type Bracket
- type Child
- type Descent
- type Equation
- func Add(left, right *Equation) *Equation
- func And(left, right *Equation) *Equation
- func ConstBool(b bool) *Equation
- func ConstFloat(f float64) *Equation
- func ConstInt(i int64) *Equation
- func ConstList(list []any) *Equation
- func ConstNil() *Equation
- func ConstNothing() *Equation
- func ConstRegex(rx *regexp.Regexp) *Equation
- func ConstString(s string) *Equation
- func Count(x Expr) *Equation
- func Divide(left, right *Equation) *Equation
- func Empty(left, right *Equation) *Equation
- func Eq(left, right *Equation) *Equation
- func Exists(left, right *Equation) *Equation
- func Get(x Expr) *Equation
- func Gt(left, right *Equation) *Equation
- func Gte(left, right *Equation) *Equation
- func Has(left, right *Equation) *Equation
- func In(left, right *Equation) *Equation
- func Length(x Expr) *Equation
- func Lt(left, right *Equation) *Equation
- func Lte(left, right *Equation) *Equation
- func Match(left, right *Equation) *Equation
- func Multiply(left, right *Equation) *Equation
- func MustParseEquation(str string) (eq *Equation)
- func Neq(left, right *Equation) *Equation
- func Not(arg *Equation) *Equation
- func Or(left, right *Equation) *Equation
- func Regex(left, right *Equation) *Equation
- func Search(left, right *Equation) *Equation
- func Sub(left, right *Equation) *Equation
- type Expr
- func A() Expr
- func B() Expr
- func C(key string) Expr
- func D() Expr
- func F(e *Equation) Expr
- func MustParse(buf []byte) (x Expr)
- func MustParseString(s string) (x Expr)
- func N(n int) Expr
- func Parse(buf []byte) (x Expr, err error)
- func ParseString(s string) (x Expr, err error)
- func R() Expr
- func S(start int, rest ...int) Expr
- func U(keys ...any) Expr
- func W() Expr
- func X() Expr
- func (x Expr) A() Expr
- func (x Expr) Append(buf []byte, brackets ...bool) []byte
- func (x Expr) At() Expr
- func (x Expr) B() Expr
- func (x Expr) BracketString() string
- func (x Expr) C(key string) Expr
- func (x Expr) Child(key string) Expr
- func (x Expr) D() Expr
- func (x Expr) Del(data any) error
- func (x Expr) DelOne(data any) error
- func (x Expr) Descent() Expr
- func (x Expr) F(e *Equation) Expr
- func (x Expr) Filter(e *Equation) Expr
- func (x Expr) First(data any) any
- func (x Expr) FirstFound(data any) (any, bool)
- func (x Expr) FirstNode(n gen.Node) (result gen.Node)
- func (x Expr) Get(data any) (results []any)
- func (x Expr) GetNodes(n gen.Node) (results []gen.Node)
- func (x Expr) Has(data any) bool
- func (x Expr) Locate(data any, max int) (locs []Expr)
- func (x Expr) Modify(data any, modifier func(element any) (altered any, changed bool)) (result any, err error)
- func (x Expr) ModifyOne(data any, modifier func(element any) (altered any, changed bool)) (result any, err error)
- func (x Expr) MustDel(data any)
- func (x Expr) MustDelOne(data any)
- func (x Expr) MustModify(data any, modifier func(element any) (altered any, changed bool)) any
- func (x Expr) MustModifyOne(data any, modifier func(element any) (altered any, changed bool)) any
- func (x Expr) MustRemove(data any) any
- func (x Expr) MustRemoveOne(data any) any
- func (x Expr) MustSet(data, value any)
- func (x Expr) MustSetOne(data, value any)
- func (x Expr) N(n int) Expr
- func (x Expr) Normal() bool
- func (x Expr) Nth(n int) Expr
- func (x Expr) R() Expr
- func (x Expr) Remove(data any) (result any, err error)
- func (x Expr) RemoveOne(data any) (result any, err error)
- func (x Expr) Root() Expr
- func (x Expr) S(start int, rest ...int) Expr
- func (x Expr) Set(data, value any) error
- func (x Expr) SetOne(data, value any) error
- func (x Expr) Slice(start int, rest ...int) Expr
- func (x Expr) String() string
- func (x Expr) U(keys ...any) Expr
- func (x Expr) Union(keys ...any) Expr
- func (x Expr) W() Expr
- func (x Expr) Wildcard() Expr
- type Filter
- type Form
- type Frag
- type Indexed
- type Keyed
- type Nth
- type Root
- type Script
- type Slice
- type Union
- type Wildcard
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Nothing can be used in scripts to indicate no value as in a script such // as [?(@.x == Nothing)] this indicates there was no value as @.x. It is // the same as [?(@.x has false)] or [?(@.x exists false)]. Nothing = nothing(0) )
Functions ¶
func AppendString ¶ added in v1.18.2
AppendString to a buffer while escaping characters as necessary.
Types ¶
type Bracket ¶
type Bracket byte
Bracket is used as a flag to indicate the path should be displayed in a bracketed representation.
type Descent ¶
type Descent byte
Descent is used as a flag to indicate the path should be displayed in a recursive descent representation.
type Equation ¶
type Equation struct {
// contains filtered or unexported fields
}
Equation represents JSON Path script and filter equations. They are used to build a script. The purpose of the Equation is to allow scripts or filters to be created without using a parser which could return an error if an invalid string representation of the script is provided.
func ConstFloat ¶
ConstFloat creates and returns an Equation for a float64 constant.
func ConstNil ¶
func ConstNil() *Equation
ConstNil creates and returns an Equation for a constant of nil.
func ConstNothing ¶ added in v1.18.0
func ConstNothing() *Equation
ConstNothing creates and returns an Equation for a constant of nothing.
func ConstRegex ¶ added in v1.14.0
ConstRegex creates and returns an Equation for a regex constant.
func ConstString ¶
ConstString creates and returns an Equation for a string constant.
func MustParseEquation ¶ added in v1.21.3
MustParseEquation parses the string argument and returns an Equation or panics.
type Expr ¶
type Expr []Frag
Expr is a JSON path expression composed of fragments. An Expr implements JSONPath as described by https://goessner.net/articles/JsonPath. Where the definition is unclear Oj has implemented the description based on the best judgement of the author.
Example (Noparse) ¶
package main import ( "fmt" "github.com/ohler55/ojg/jp" "github.com/ohler55/ojg/oj" ) func main() { data := map[string]any{ "a": []any{ map[string]any{"x": 1, "y": 2, "z": 3}, map[string]any{"x": 1, "y": 4, "z": 9}, }, "b": []any{ map[string]any{"x": 4, "y": 5, "z": 6}, map[string]any{"x": 16, "y": 25, "z": 36}, }, } x := jp.C("b").F(jp.Gt(jp.Get(jp.A().C("y")), jp.ConstInt(10))).C("x") fmt.Println(x.String()) result := x.Get(data) fmt.Println(oj.JSON(result, &oj.Options{Sort: true})) }
Output: b[?(@.y > 10)].x [16]
func MustParseString ¶ added in v1.11.0
MustParseString parses a string into an Expr and panics on error.
Example ¶
package main import ( "fmt" "github.com/ohler55/ojg/jp" "github.com/ohler55/ojg/oj" ) func main() { data := map[string]any{ "a": []any{ map[string]any{"x": 1, "y": 2, "z": 3}, map[string]any{"x": 1, "y": 4, "z": 9}, }, "b": []any{ map[string]any{"x": 4, "y": 5, "z": 6}, map[string]any{"x": 16, "y": 25, "z": 36}, }, } x := jp.MustParseString("b[?(@.y > 10)].x") fmt.Println(x.String()) result := x.Get(data) fmt.Println(oj.JSON(result)) }
Output: b[?(@.y > 10)].x [16]
func ParseString ¶
ParseString parses a string into an Expr.
Example ¶
package main import ( "fmt" "github.com/ohler55/ojg/jp" "github.com/ohler55/ojg/oj" ) func main() { data := map[string]any{ "a": []any{ map[string]any{"x": 1, "y": 2, "z": 3}, map[string]any{"x": 1, "y": 4, "z": 9}, }, "b": []any{ map[string]any{"x": 4, "y": 5, "z": 6}, map[string]any{"x": 16, "y": 25, "z": 36}, }, } x, err := jp.ParseString("b[?(@.y > 10)].x") if err != nil { fmt.Println(err.Error()) return } fmt.Println(x.String()) result := x.Get(data) fmt.Println(oj.JSON(result)) }
Output: b[?(@.y > 10)].x [16]
func (Expr) Append ¶
Append a string representation of the expression to a byte slice and return the expanded buffer.
func (Expr) BracketString ¶ added in v1.21.0
BracketString returns a string representation of the expression using the bracket notation.
func (Expr) Del ¶
Del removes matching nodes.
Example ¶
package main import ( "fmt" "github.com/ohler55/ojg" "github.com/ohler55/ojg/jp" "github.com/ohler55/ojg/sen" ) func main() { data := []any{ map[string]any{"a": 1, "b": 2, "c": 3}, } if err := jp.N(0).C("b").Del(data); err != nil { panic(err) } fmt.Println(sen.String(data, &ojg.Options{Sort: true})) }
Output: [{a:1 c:3}]
func (Expr) FirstFound ¶ added in v1.18.0
FirstFound element of the data identified by the path.
func (Expr) Has ¶ added in v1.13.0
Has returns true if there is a value ot the end of the path specified. A nil value is still a value.
func (Expr) Locate ¶ added in v1.21.0
Locate the values described by the Expr and return a slice of normalized paths to those values in the data. The returned slice is limited to the max specified. A max of 0 or less indicates there is no maximum.
func (Expr) Modify ¶ added in v1.17.0
func (x Expr) Modify(data any, modifier func(element any) (altered any, changed bool)) (result any, err error)
Modify modifies matching nodes and panics on an expression error. Modified elements replace the original element in the data. The modified data is returned. Unless the data is a slice and modified the returned data will be the same object as the original. The modifier function will be called with the elements that match the path and should return the original element or if altered the altered value along with setting the returned changed value to true.
func (Expr) ModifyOne ¶ added in v1.17.0
func (x Expr) ModifyOne(data any, modifier func(element any) (altered any, changed bool)) (result any, err error)
ModifyOne modifies matching nodes and panics on an expression error. Modified elements replace the original element in the data. The modified data is returned. Unless the data is a slice and modified the returned data will be the same object as the original. The modifier function will be called with the elements that match the path and should return the original element or if altered the altered value along with setting the returned changed value to true. The function returns after the first modification.
func (Expr) MustDel ¶ added in v1.11.0
MustDel removes matching nodes and pinics on error.
Example ¶
package main import ( "fmt" "github.com/ohler55/ojg" "github.com/ohler55/ojg/jp" "github.com/ohler55/ojg/sen" ) func main() { data := []any{ map[string]any{"a": 1, "b": 2, "c": 3}, } jp.N(0).C("b").MustDel(data) fmt.Println(sen.String(data, &ojg.Options{Sort: true})) }
Output: [{a:1 c:3}]
func (Expr) MustDelOne ¶ added in v1.16.0
MustDelOne removes one matching node and pinics on error.
func (Expr) MustModify ¶ added in v1.17.0
MustModify modifies matching nodes and panics on an expression error. Modified elements replace the original element in the data. The modified data is returned. Unless the data is a slice and modified the returned data will be the same object as the original. The modifier function will be called with the elements that match the path and should return the original element or if altered the altered value along with setting the returned changed value to true.
func (Expr) MustModifyOne ¶ added in v1.17.0
MustModifyOne modifies matching nodes and panics on an expression error. Modified elements replace the original element in the data. The modified data is returned. Unless the data is a slice and modified the returned data will be the same object as the original. The modifier function will be called with the elements that match the path and should return the original element or if altered the altered value along with setting the returned changed value to true. The function returns after the first modification.
func (Expr) MustRemove ¶ added in v1.16.0
MustRemove removes matching nodes and panics on an expression error but silently makes no changes if there is no match for the expression. Removed slice elements are removed and the remaining elements are moveed to fill in the removed element. The slice is shortened.
func (Expr) MustRemoveOne ¶ added in v1.16.0
MustRemoveOne removes matching nodes and panics on an expression error but silently makes no changes if there is no match for the expression. Removed slice elements are removed and the remaining elements are moveed to fill in the removed element. The slice is shortened.
func (Expr) MustSet ¶ added in v1.11.0
MustSet all matching child node values. If the path to the child does not exist array and map elements are added. Panics on error.
Example ¶
package main import ( "fmt" "github.com/ohler55/ojg" "github.com/ohler55/ojg/jp" "github.com/ohler55/ojg/sen" ) func main() { data := []any{ map[string]any{"a": 1, "b": 2, "c": 3}, } // Set members with a JSONPath expression. jp.N(0).C("b").MustSet(data, 7) fmt.Println(sen.String(data, &ojg.Options{Sort: true})) // Add members with a JSONPath expression. jp.N(0).C("d").MustSet(data, 4) fmt.Println(sen.String(data, &ojg.Options{Sort: true})) }
Output: [{a:1 b:7 c:3}] [{a:1 b:7 c:3 d:4}]
func (Expr) MustSetOne ¶ added in v1.16.0
MustSetOne child node value. If the path to the child does not exist array and map elements are added. Panics on error.
func (Expr) Normal ¶ added in v1.21.2
Normal returns true if the only fragments in the expression are root, at, child, and nth.
func (Expr) Remove ¶ added in v1.16.0
Remove removes matching nodes. An error is returned for an expression error but silently makes no changes if there is no match for the expression. Removed slice elements are removed and the remaining elements are moveed to fill in the removed element. The slice is shortened.
func (Expr) RemoveOne ¶ added in v1.16.0
RemoveOne removes at most one node. An error is returned for an expression error but silently makes no changes if there is no match for the expression. Removed slice elements are removed and the remaining elements are moveed to fill in the removed element. The slice is shortened.
func (Expr) Set ¶
Set all matching child node values. An error is returned if it is not possible. If the path to the child does not exist array and map elements are added.
Example ¶
package main import ( "fmt" "github.com/ohler55/ojg" "github.com/ohler55/ojg/jp" "github.com/ohler55/ojg/sen" ) func main() { data := []any{ map[string]any{"a": 1, "b": 2, "c": 3}, } // Set members with a JSONPath expression. if err := jp.N(0).C("b").Set(data, 7); err != nil { panic(err) } fmt.Println(sen.String(data, &ojg.Options{Sort: true})) // Add members with a JSONPath expression. if err := jp.N(0).C("d").Set(data, 4); err != nil { panic(err) } fmt.Println(sen.String(data, &ojg.Options{Sort: true})) }
Output: [{a:1 b:7 c:3}] [{a:1 b:7 c:3 d:4}]
func (Expr) SetOne ¶
SetOne child node value. An error is returned if it is not possible. If the path to the child does not exist array and map elements are added.
type Filter ¶
type Filter struct {
Script
}
Filter is a script used as a filter.
func MustNewFilter ¶ added in v1.11.0
MustNewFilter creates a new Filter and panics on error.
type Form ¶ added in v1.15.0
type Form struct { // Op is the operation to perform. Op string // Left is the left side a form. The type can be a *Form, Expr, or any of // the simple types. Left any // Right is the left side a form. The type can be a *Form, Expr, or any of // the simple types. Right any }
Form represents a component of a JSON Path script and filter. They are used inspect a Script or Filter. The general template for a Form is (left op right). For an operations such as not (!) the right side is left as nil. As an example a Filter fragment of [?(@.x == 3)] whould be representing in a Form as
Form{Op: "==", Left: jp.Expr{jp.At('@'), jp.Child("x")}, Right: 3}.
type Frag ¶
type Frag interface { // Append a fragment string representation of the fragment to the buffer // then returning the expanded buffer. Append(buf []byte, bracket, first bool) []byte // contains filtered or unexported methods }
Frag represents a JSONPath fragment. A JSONPath expression is composed of fragments (Frag) linked together to form a full path expression.
type Indexed ¶ added in v1.18.2
type Indexed interface { // ValueAtIndex should return the value at the provided index or nil if no // entry exists at the index. ValueAtIndex(index int) any // SetValueAtIndex should set the value at the provided index. SetValueAtIndex(index int, value any) // Size should return the size for the collection. Size() int }
Indexed describes an interface for a collection that is indexed by a integers similar to a []any.
type Keyed ¶ added in v1.18.2
type Keyed interface { // ValueForKey should return the value associated with the key or nil if // no entry exists for the key. ValueForKey(key string) (value any, has bool) // SetValueForKey sets the value for a key in the collection. SetValueForKey(key string, value any) // RemoveValueForKey removes the value for a key in the collection. RemoveValueForKey(key string) // Keys should return an list of the keys for all the entries in the // collection. Keys() []string }
Keyed describes an interface for a collection that is indexed by a string key similar to a map[string]any.
type Nth ¶
type Nth int
Nth is a subscript operator that matches the n-th element in an array for a JSON path expression.
type Script ¶
type Script struct {
// contains filtered or unexported fields
}
Script represents JSON Path script used in filters as well.
Example ¶
package main import ( "fmt" "github.com/ohler55/ojg/jp" "github.com/ohler55/ojg/oj" ) func main() { data := []any{ map[string]any{"a": 1, "b": 2, "c": 3}, map[string]any{"a": int64(52), "b": 4, "c": 6}, } // Build an Equation and generate a Script from the Equation. s := jp.Or( jp.Lt(jp.Get(jp.A().C("a")), jp.ConstInt(52)), jp.Eq(jp.Get(jp.A().C("x")), jp.ConstString("cool")), ).Script() fmt.Println(s.String()) // Normally Scripts are using in Expr (JSON paths). result := s.Eval([]any{}, data) fmt.Println(oj.JSON(result, &oj.Options{Sort: true})) }
Output: (@.a < 52 || @.x == 'cool') [{"a":1,"b":2,"c":3}]
func MustNewScript ¶ added in v1.11.0
MustNewScript parses the string argument and returns a script or an error.
func (*Script) Append ¶
Append a string representation of the fragment to the buffer and then return the expanded buffer.
type Union ¶
type Union []any
Union is a union operation for a JSON path expression which is a union of a Child and Nth fragment.