jsonpath

package module
v0.0.0-...-8b5263e Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2022 License: Artistic-2.0 Imports: 10 Imported by: 0

README

JsonPath

A golang implementation of JsonPath syntax. follow the majority rules in http://goessner.net/articles/JsonPath/ but also with some minor differences.

Golang Version Required: 1.5+

Get Started

go get github.com/easyrasta/jsonpath

example code:

import (
    "github.com/easyrasta/jsonpath"
    "encoding/json"
)

var json_data interface{}
json.Unmarshal([]byte(data), &json_data)
res, err := jsonpath.JsonPathLookup(json_data, "$.expensive")

Operators

referenced from github.com/jayway/JsonPath

Operator Supported Description
$ Y The root element to query. This starts all path expressions.
@ Y The current node being processed by a filter predicate.
* Y Wildcard. Available anywhere a name or numeric are required.
.. X Deep scan. Available anywhere a name is required.
. Y Dot-notated child
['' (, '')] X Bracket-notated child or children
[ (, )] Y Array index or indexes
[start:end] Y Array slice operator
[?()] Y Filter expression. Expression must evaluate to a boolean value.

Examples

given these example data.

{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

example json path syntax.

jsonpath result
$.expensive 10
$.store.book[0].price 8.95
$.store.book[-1].isbn "0-395-19395-8"
$.store.book[0,1].price [8.95, 12.99]
$.store.book[0:2].price [8.95, 12.99, 8.99]
$.store.book[?(@.isbn)].price [8.99, 22.99]
$.store.book[?(@.price > 10)].title ["Sword of Honour", "The Lord of the Rings"]
$.store.book[?(@.price < $.expensive)].price [8.95, 8.99]
$.store.book[:].price [8.9.5, 12.99, 8.9.9, 22.99]

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	MapTypeError      = errors.New("Expected Type to be a Map.")
	ArrayTypeError    = errors.New("Expected Type to be an Array.")
	SyntaxError       = errors.New("Bad Syntax.")
	NotSupportedError = errors.New("Not Supported")
	NotFound          = errors.New("Not Found")
	IndexOutOfBounds  = errors.New("Out of Bounds")
)

Functions

This section is empty.

Types

type Applicator

type Applicator interface {
	Apply(v interface{}) (interface{}, error)
}

func Parse

func Parse(s string) (Applicator, error)

Parse parses the JSONPath and returns a object that can be applied to a structure to filter it down.

type ArraySelection

type ArraySelection struct {
	Key int
	RootNode
}

ArrySelection is a the basic filter for a Array type key. It is like MapSelection but for Arrays.

func (*ArraySelection) Apply

func (a *ArraySelection) Apply(v interface{}) (interface{}, error)

type DescentSelection

type DescentSelection struct {
	RootNode
}

DescentSelection is a filter that recursively descends applying it's NextNode and corrlating the results.

func (*DescentSelection) Apply

func (d *DescentSelection) Apply(v interface{}) (interface{}, error)

type MapSelection

type MapSelection struct {
	Key string
	RootNode
}

MapSelection is a the basic filter for a Map type key. It will look at the in coming v and try to turn it into a map[string]interface{} value. If it successeeds it will then apply the NextNode to that interface value, or it will return the value if it has no NextNode.

func (*MapSelection) Apply

func (m *MapSelection) Apply(v interface{}) (interface{}, error)

type RootNode

type RootNode struct {
	// The next Node in the sequence.
	NextNode node
}

RootNode is always the top node. It does not really do anything other then delegate to the next node, and acts as a starting point. Every other node type embeds this node To get the NextNode functions.

func (*RootNode) Apply

func (r *RootNode) Apply(v interface{}) (interface{}, error)

Apply is the main workhourse, each node type will apply it's filtering rules to the provided value, returning the filtered result. It is expected that the node will call's it's Next Nodes Apply method as need by the rules of the Node.

func (*RootNode) SetNext

func (r *RootNode) SetNext(n node)

Set the next node.

type WildCardFilterSelection

type WildCardFilterSelection struct {
	RootNode
	Key string
}

func (*WildCardFilterSelection) Apply

func (w *WildCardFilterSelection) Apply(v interface{}) (interface{}, error)

func (*WildCardFilterSelection) GetConditionsFromKey

func (w *WildCardFilterSelection) GetConditionsFromKey() ([]string, error)

type WildCardKeySelection

type WildCardKeySelection struct {
	RootNode
}

func (*WildCardKeySelection) Apply

func (w *WildCardKeySelection) Apply(v interface{}) (interface{}, error)

type WildCardSelection

type WildCardSelection struct {
	RootNode
}

WildCardSelection is a filter that grabs all the values and returns an Array of them It applies it's NextNode on each value.

func (*WildCardSelection) Apply

func (w *WildCardSelection) Apply(v interface{}) (interface{}, error)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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