Documentation
¶
Overview ¶
Package jsonpath is for retrieving a part of JSON according to the JSONPath query syntax.
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/AsaiYusuke/jsonpath"
)
func main() {
jsonPath, srcJSON := `$.key`, `{"key":"value"}`
var src interface{}
json.Unmarshal([]byte(srcJSON), &src)
output, _ := jsonpath.Retrieve(jsonPath, src)
outputJSON, _ := json.Marshal(output)
fmt.Println(string(outputJSON))
}
Output: ["value"]
Index ¶
- func Parse(jsonPath string, config ...Config) (f func(src interface{}) ([]interface{}, error), err error)
- func Pretty(pretty bool) func(*pegJSONPathParser) error
- func Retrieve(jsonPath string, src interface{}, config ...Config) ([]interface{}, error)
- func Size(size int) func(*pegJSONPathParser) error
- type Accessor
- type Config
- type ErrorFunctionFailed
- type ErrorFunctionNotFound
- type ErrorInvalidArgument
- type ErrorInvalidSyntax
- type ErrorMemberNotExist
- type ErrorNotSupported
- type ErrorTypeUnmatched
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
func Parse(jsonPath string, config ...Config) (f func(src interface{}) ([]interface{}, error), err error)
Parse returns the parser function using the given JSONPath.
Example ¶
package main
import (
"encoding/json"
"fmt"
"reflect"
"github.com/AsaiYusuke/jsonpath"
)
func main() {
jsonPath := `$.key`
srcJSON1 := `{"key":"value1"}`
srcJSON2 := `{"key":"value2"}`
jsonPathParser, err := jsonpath.Parse(jsonPath)
if err != nil {
fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
return
}
var src1, src2 interface{}
json.Unmarshal([]byte(srcJSON1), &src1)
json.Unmarshal([]byte(srcJSON2), &src2)
output1, err := jsonPathParser(src1)
if err != nil {
fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
return
}
output2, err := jsonPathParser(src2)
if err != nil {
fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
return
}
outputJSON1, _ := json.Marshal(output1)
outputJSON2, _ := json.Marshal(output2)
fmt.Println(string(outputJSON1))
fmt.Println(string(outputJSON2))
}
Output: ["value1"] ["value2"]
func Retrieve ¶
Retrieve returns the retrieved JSON using the given JSONPath.
Example ¶
package main
import (
"encoding/json"
"fmt"
"reflect"
"github.com/AsaiYusuke/jsonpath"
)
func main() {
jsonPath, srcJSON := `$.key`, `{"key":"value"}`
var src interface{}
json.Unmarshal([]byte(srcJSON), &src)
output, err := jsonpath.Retrieve(jsonPath, src)
if err != nil {
fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
return
}
outputJSON, _ := json.Marshal(output)
fmt.Println(string(outputJSON))
}
Output: ["value"]
Types ¶
type Accessor ¶ added in v1.2.0
type Accessor struct {
Get func() interface{}
Set func(interface{})
}
Accessor represents the accessor to the result nodes of JSONPath.
type Config ¶ added in v1.1.0
type Config struct {
// contains filtered or unexported fields
}
Config represents the configuration parameters.
func (*Config) SetAccessorMode ¶ added in v1.2.0
func (c *Config) SetAccessorMode()
SetAccessorMode sets a collection of accessors to the result.
Example ¶
package main
import (
"encoding/json"
"fmt"
"reflect"
"github.com/AsaiYusuke/jsonpath"
)
func main() {
config := jsonpath.Config{}
config.SetAccessorMode()
jsonPath, srcJSON := `$.a`, `{"a":1,"b":0}`
var src interface{}
json.Unmarshal([]byte(srcJSON), &src)
output, err := jsonpath.Retrieve(jsonPath, src, config)
if err != nil {
fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
return
}
accessor := output[0].(jsonpath.Accessor)
srcMap := src.(map[string]interface{})
fmt.Printf("Get : %v\n", accessor.Get())
accessor.Set(2)
fmt.Printf("Set -> Src : %v\n", srcMap[`a`])
accessor.Set(3)
fmt.Printf("Set -> Get : %v\n", accessor.Get())
srcMap[`a`] = 4
fmt.Printf("Src -> Get : %v\n", accessor.Get())
}
Output: Get : 1 Set -> Src : 2 Set -> Get : 3 Src -> Get : 4
func (*Config) SetAggregateFunction ¶ added in v1.1.0
SetAggregateFunction sets the custom function.
Example ¶
package main
import (
"encoding/json"
"fmt"
"reflect"
"github.com/AsaiYusuke/jsonpath"
)
func main() {
config := jsonpath.Config{}
config.SetAggregateFunction(`max`, func(params []interface{}) (interface{}, error) {
var result float64
for _, param := range params {
if floatParam, ok := param.(float64); ok {
if result < floatParam {
result = floatParam
}
continue
}
return nil, fmt.Errorf(`type error`)
}
return result, nil
})
jsonPath, srcJSON := `$[*].max()`, `[1,3]`
var src interface{}
json.Unmarshal([]byte(srcJSON), &src)
output, err := jsonpath.Retrieve(jsonPath, src, config)
if err != nil {
fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
return
}
outputJSON, _ := json.Marshal(output)
fmt.Println(string(outputJSON))
}
Output: [3]
func (*Config) SetFilterFunction ¶ added in v1.1.0
SetFilterFunction sets the custom function.
Example ¶
package main
import (
"encoding/json"
"fmt"
"reflect"
"github.com/AsaiYusuke/jsonpath"
)
func main() {
config := jsonpath.Config{}
config.SetFilterFunction(`twice`, func(param interface{}) (interface{}, error) {
if floatParam, ok := param.(float64); ok {
return floatParam * 2, nil
}
return nil, fmt.Errorf(`type error`)
})
jsonPath, srcJSON := `$[*].twice()`, `[1,3]`
var src interface{}
json.Unmarshal([]byte(srcJSON), &src)
output, err := jsonpath.Retrieve(jsonPath, src, config)
if err != nil {
fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
return
}
outputJSON, _ := json.Marshal(output)
fmt.Println(string(outputJSON))
}
Output: [2,6]
type ErrorFunctionFailed ¶ added in v1.1.0
type ErrorFunctionFailed struct {
// contains filtered or unexported fields
}
ErrorFunctionFailed represents the error that the function specified in the JSONPath failed.
Example ¶
package main
import (
"encoding/json"
"fmt"
"reflect"
"github.com/AsaiYusuke/jsonpath"
)
func main() {
config := jsonpath.Config{}
config.SetFilterFunction(`invalid`, func(param interface{}) (interface{}, error) {
return nil, fmt.Errorf(`invalid function executed`)
})
jsonPath, srcJSON := `$.invalid()`, `{}`
var src interface{}
json.Unmarshal([]byte(srcJSON), &src)
output, err := jsonpath.Retrieve(jsonPath, src, config)
if err != nil {
fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
return
}
outputJSON, _ := json.Marshal(output)
fmt.Println(string(outputJSON))
}
Output: jsonpath.ErrorFunctionFailed, function failed (function=.invalid(), error=invalid function executed)
func (ErrorFunctionFailed) Error ¶ added in v1.1.0
func (e ErrorFunctionFailed) Error() string
type ErrorFunctionNotFound ¶ added in v1.1.0
type ErrorFunctionNotFound struct {
// contains filtered or unexported fields
}
ErrorFunctionNotFound represents the error that the function specified in the JSONPath is not found.
Example ¶
package main
import (
"encoding/json"
"fmt"
"reflect"
"github.com/AsaiYusuke/jsonpath"
)
func main() {
jsonPath, srcJSON := `$.unknown()`, `{}`
var src interface{}
json.Unmarshal([]byte(srcJSON), &src)
output, err := jsonpath.Retrieve(jsonPath, src)
if err != nil {
fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
return
}
outputJSON, _ := json.Marshal(output)
fmt.Println(string(outputJSON))
}
Output: jsonpath.ErrorFunctionNotFound, function not found (function=.unknown())
func (ErrorFunctionNotFound) Error ¶ added in v1.1.0
func (e ErrorFunctionNotFound) Error() string
type ErrorInvalidArgument ¶
type ErrorInvalidArgument struct {
// contains filtered or unexported fields
}
ErrorInvalidArgument represents the error that argument specified in the JSONPath is treated as the invalid error in Go syntax.
Example ¶
package main
import (
"encoding/json"
"fmt"
"reflect"
"github.com/AsaiYusuke/jsonpath"
)
func main() {
jsonPath, srcJSON := `$[?(1.0.0>0)]`, `{}`
var src interface{}
json.Unmarshal([]byte(srcJSON), &src)
output, err := jsonpath.Retrieve(jsonPath, src)
if err != nil {
fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
return
}
outputJSON, _ := json.Marshal(output)
fmt.Println(string(outputJSON))
}
Output: jsonpath.ErrorInvalidArgument, invalid argument (argument=1.0.0, error=strconv.ParseFloat: parsing "1.0.0": invalid syntax)
func (ErrorInvalidArgument) Error ¶
func (e ErrorInvalidArgument) Error() string
type ErrorInvalidSyntax ¶
type ErrorInvalidSyntax struct {
// contains filtered or unexported fields
}
ErrorInvalidSyntax represents the error that have syntax error in the JSONPath.
Example ¶
package main
import (
"encoding/json"
"fmt"
"reflect"
"github.com/AsaiYusuke/jsonpath"
)
func main() {
jsonPath, srcJSON := `$.`, `{}`
var src interface{}
json.Unmarshal([]byte(srcJSON), &src)
output, err := jsonpath.Retrieve(jsonPath, src)
if err != nil {
fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
return
}
outputJSON, _ := json.Marshal(output)
fmt.Println(string(outputJSON))
}
Output: jsonpath.ErrorInvalidSyntax, invalid syntax (position=1, reason=unrecognized input, near=.)
func (ErrorInvalidSyntax) Error ¶
func (e ErrorInvalidSyntax) Error() string
type ErrorMemberNotExist ¶
type ErrorMemberNotExist struct {
// contains filtered or unexported fields
}
ErrorMemberNotExist represents the error that the member specified in the JSONPath did not exist in the JSON object.
Example ¶
package main
import (
"encoding/json"
"fmt"
"reflect"
"github.com/AsaiYusuke/jsonpath"
)
func main() {
jsonPath, srcJSON := `$.none`, `{}`
var src interface{}
json.Unmarshal([]byte(srcJSON), &src)
output, err := jsonpath.Retrieve(jsonPath, src)
if err != nil {
fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
return
}
outputJSON, _ := json.Marshal(output)
fmt.Println(string(outputJSON))
}
Output: jsonpath.ErrorMemberNotExist, member did not exist (path=.none)
func (ErrorMemberNotExist) Error ¶
func (e ErrorMemberNotExist) Error() string
type ErrorNotSupported ¶
type ErrorNotSupported struct {
// contains filtered or unexported fields
}
ErrorNotSupported represents the error that the unsupported syntaxes specified in the JSONPath.
Example ¶
package main
import (
"encoding/json"
"fmt"
"reflect"
"github.com/AsaiYusuke/jsonpath"
)
func main() {
jsonPath, srcJSON := `$[(command)]`, `{}`
var src interface{}
json.Unmarshal([]byte(srcJSON), &src)
output, err := jsonpath.Retrieve(jsonPath, src)
if err != nil {
fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
return
}
outputJSON, _ := json.Marshal(output)
fmt.Println(string(outputJSON))
}
Output: jsonpath.ErrorNotSupported, not supported (feature=script, path=[(command)])
func (ErrorNotSupported) Error ¶
func (e ErrorNotSupported) Error() string
type ErrorTypeUnmatched ¶
type ErrorTypeUnmatched struct {
// contains filtered or unexported fields
}
ErrorTypeUnmatched represents the error that the node type specified in the JSONPath did not exist in the JSON object.
Example ¶
package main
import (
"encoding/json"
"fmt"
"reflect"
"github.com/AsaiYusuke/jsonpath"
)
func main() {
jsonPath, srcJSON := `$.a`, `[]`
var src interface{}
json.Unmarshal([]byte(srcJSON), &src)
output, err := jsonpath.Retrieve(jsonPath, src)
if err != nil {
fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
return
}
outputJSON, _ := json.Marshal(output)
fmt.Println(string(outputJSON))
}
Output: jsonpath.ErrorTypeUnmatched, type unmatched (expected=object, found=[]interface {}, path=.a)
func (ErrorTypeUnmatched) Error ¶
func (e ErrorTypeUnmatched) Error() string
Source Files
¶
- accessor.go
- buffer_container.go
- cache.go
- config.go
- constants.go
- error_basic_runtime.go
- error_checksyntax_function_not_found.go
- error_checksyntax_invalid_argument.go
- error_checksyntax_invalid_syntax.go
- error_checksyntax_not_supported.go
- error_if_runtime.go
- error_runtime_function_failed.go
- error_runtime_member_not_exist.go
- error_runtime_type_unmatched.go
- jsonpath.go
- jsonpath.peg.go
- jsonpath_doc.go
- jsonpath_parser.go
- syntax_basic_comparator_any_value.go
- syntax_basic_comparator_numeric.go
- syntax_basic_comparator_string.go
- syntax_basic_compare_parameter.go
- syntax_basic_compare_query.go
- syntax_basic_node.go
- syntax_basic_subscript.go
- syntax_if_comparator.go
- syntax_if_node.go
- syntax_if_query.go
- syntax_if_query_jsonpath_parameter.go
- syntax_if_subscript.go
- syntax_node_function_aggregate.go
- syntax_node_function_filter.go
- syntax_node_identifier_child_multi.go
- syntax_node_identifier_child_single.go
- syntax_node_identifier_child_wildcard.go
- syntax_node_identifier_current_root.go
- syntax_node_identifier_recursive_child.go
- syntax_node_identifier_root.go
- syntax_node_qualifier_filter.go
- syntax_node_qualifier_union.go
- syntax_query_compare_comparator_eq.go
- syntax_query_compare_comparator_ge.go
- syntax_query_compare_comparator_gt.go
- syntax_query_compare_comparator_le.go
- syntax_query_compare_comparator_lt.go
- syntax_query_compare_comparator_regex.go
- syntax_query_logical_and.go
- syntax_query_logical_not.go
- syntax_query_logical_or.go
- syntax_query_param_current_root.go
- syntax_query_param_literal.go
- syntax_query_param_root.go
- syntax_subscript_index.go
- syntax_subscript_slice_negative_step.go
- syntax_subscript_slice_positive_step.go
- syntax_subscript_wildcard.go