jsonpath

package
v0.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2023 License: BSD-3-Clause, BSD-3-Clause Imports: 7 Imported by: 0

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/kubemod/kubemod/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

func Get(path string, value interface{}) (interface{}, error)

Get executes given JSONPath on given value

Example
package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/kubemod/kubemod/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/kubemod/kubemod/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/kubemod/kubemod/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 IsUndefined

func IsUndefined(v interface{}) bool

func Language

func Language() gval.Language

Language is the JSONPath Language

func New

func New(path string) (gval.Evaluable, error)

New returns an selector for given JSONPath

func PlaceholderExtension

func PlaceholderExtension() gval.Language

PlaceholderExtension is the JSONPath Language with placeholder

Types

type UndefinedType

type UndefinedType float64

KubeMod modification to the original language {Begin} UndefinedType represents the type of the Undefined package.

var (
	Undefined UndefinedType = UndefinedType(math.NaN())
)

func (UndefinedType) String

func (UndefinedType) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL