Documentation
¶
Overview ¶
Package jsonpath is an implementation of http://goessner.net/articles/JsonPath/ If a JSONPath contains one of [key1, key2 ...], .., *, [min:max], [min:max:step], (? expression) all matchs are listed in an []interface{}
The package comes with an extension of JSONPath to access the wildcard values of a match. If the JSONPath is used inside of a JSON object, you can use placeholder '#' or '#i' with natural number i to access all wildcards values or the ith wildcard
This package can be extended with gval modules for script features like multiply, length, regex or many more. So take a look at github.com/PaesslerAG/gval.
Example (Gval) ¶
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/PaesslerAG/gval"
"github.com/PaesslerAG/jsonpath"
)
func main() {
builder := gval.Full(jsonpath.PlaceholderExtension())
path, err := builder.NewEvaluable("{#1: $..[?@.ping && @.speed > 100].name}")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
v := interface{}(nil)
err = json.Unmarshal([]byte(`{
"device 1":{
"name": "fancy device",
"ping": true,
"speed": 200,
"subdevice 1":{
"ping" : true,
"speed" : 99,
"name" : "boring subdevice"
},
"subdevice 2":{
"ping" : true,
"speed" : 150,
"name" : "fancy subdevice"
},
"not an device":{
"name" : "ping me but I have no speed property",
"ping" : true
}
},
"fictive device":{
"ping" : false,
"speed" : 1000,
"name" : "dream device"
}
}`), &v)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
devices, err := path(context.Background(), v)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for device, name := range devices.(map[string]interface{}) {
fmt.Printf("%s -> %v\n", device, name)
}
}
Output: device 1 -> fancy device subdevice 2 -> fancy subdevice
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Get ¶
Get executes given JSONPath on given value
Example ¶
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/PaesslerAG/jsonpath"
)
func main() {
v := interface{}(nil)
json.Unmarshal([]byte(`{
"welcome":{
"message":["Good Morning", "Hello World!"]
}
}`), &v)
welcome, err := jsonpath.Get("$.welcome.message[1]", v)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(welcome)
}
Output: Hello World!
Example (Filter) ¶
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/PaesslerAG/jsonpath"
)
func main() {
v := interface{}(nil)
json.Unmarshal([]byte(`[
{"key":"a","value" : "I"},
{"key":"b","value" : "II"},
{"key":"c","value" : "III"}
]`), &v)
values, err := jsonpath.Get(`$[? @.key=="b"].value`, v)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, value := range values.([]interface{}) {
fmt.Println(value)
}
}
Output: II
Example (Wildcard) ¶
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/PaesslerAG/jsonpath"
)
func main() {
v := interface{}(nil)
json.Unmarshal([]byte(`{
"welcome":{
"message":["Good Morning", "Hello World!"]
}
}`), &v)
welcome, err := jsonpath.Get("$.welcome.message[*]", v)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, value := range welcome.([]interface{}) {
fmt.Printf("%v\n", value)
}
}
Output: Good Morning Hello World!
func PlaceholderExtension ¶ added in v0.1.1
PlaceholderExtension is the JSONPath Language with placeholder
Types ¶
This section is empty.
