jsonpath

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package jsonpath implements Stefan Goener's JSONPath http://goessner.net/articles/JsonPath/

A jsonpath applies to any JSON decoded data using interface{} when decoded with encoding/json (http://golang.org/pkg/encoding/json/) :

var bookstore interface{}
err := json.Unmarshal(data, &bookstore)
authors, err := jsonpath.Read(bookstore, "$..authors")

A jsonpath expression can be prepared to be reused multiple times :

allAuthors, err := jsonpath.Prepare("$..authors")
...
var bookstore interface{}
err = json.Unmarshal(data, &bookstore)
authors, err := allAuthors(bookstore)

The type of the values returned by the `Read` method or `Prepare` functions depends on the jsonpath expression.

Limitations

No support for subexpressions and filters. Strings in brackets must use double quotes. It cannot operate on JSON decoded struct fields.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Find

func Find(j interface{}, jpath string) interface{}

func FindFirst

func FindFirst(j interface{}, jpath string) interface{}

func Read

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

Read a path from a decoded JSON array or object ([]interface{} or map[string]interface{}) and returns the corresponding value or an error.

The returned value type depends on the requested path and the JSON value.

func RecursiveDeepJsonPath

func RecursiveDeepJsonPath(i any) []string

func RecursiveDeepReplace

func RecursiveDeepReplace(i any, val any) []any

func RecursiveDeepReplaceString

func RecursiveDeepReplaceString(i string, val any) []string

func Replace

func Replace(origin any, path string, replaceValue interface{}) (map[string]interface{}, error)

Prepare a path for reuse with multiple JSON values.

func ReplaceAll

func ReplaceAll(j interface{}, jpath string, replaceValue interface{}) map[string]interface{}

func ReplaceEx added in v1.2.2

func ReplaceEx(origin any, path string, replaceValue interface{}) (any, error)

ReplaceEx replace the value of the path in origin

func ReplaceString

func ReplaceString(i string, jp string, replaced any) string

func ToMapInterface

func ToMapInterface(origin any) (map[string]interface{}, any, error)

Types

type FilterFunc

type FilterFunc func(value interface{}) (interface{}, error)

FilterFunc applies a prepared json path to a JSON decoded value

func Prepare

func Prepare(path string) (FilterFunc, error)

Prepare will parse the path and return a filter function that can then be applied to decoded JSON values.

Jump to

Keyboard shortcuts

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