jsonpath

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2023 License: MIT Imports: 7 Imported by: 1

README

jsonpath

Minimalistic implementation of jsonpath, based on http://goessner.net/articles/JsonPath/.

But a better grammar definition is here.

Documentation

https://godoc.org/github.com/gobs/jsonpath

Usage

Install:

go get github.com/gobs/jsonpath

And use it as a library:

package main

import "github.com/gobs/jsonpath"
import "fmt"

func main() {
    jsonpathexpr := "$.items[3].stuff"

    p := jsonpath.NewProcessor()
    if !p.Parse(jsonpathexpr) {
        return
    }

    // loaded from JSON
    input := map[string]interface{}{
        "name": "joe",
        "items": []interface{}{
            "zero",
            "one",
            "two",
            map[string]interface{}{
                "stuff": "three",
            },
        },
    }

    ret := p.Process(input, jsonpath.Default)
    fmt.Println(ret)
}

Or install the command:

go get github.com/gobs/jsonpath/cmd/jsonpath

And run it:

jsonpath "$.items[3].stuff" input.json

Documentation

Index

Constants

View Source
const (
	ARRAY_RANGE nodeType = iota
	ARRAY_ITEMS
	CHILD
	DESCENDANT
	ALL_ITEMS
	FILTER_EXPR
	SCRIPT_EXPR

	MAX_RANGE = 0x7ffffff

	TOKEN_ANY    = "*"
	TOKEN_LENGTH = "!" // not a valid identifiers
	TOKEN_REGEX  = "=~"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node defines a processing node

func (*Node) CompareEqual

func (n *Node) CompareEqual(v interface{}) interface{}

func (*Node) CompareExists

func (n *Node) CompareExists(v interface{}) interface{}

func (*Node) CompareGreater

func (n *Node) CompareGreater(v interface{}) interface{}

func (*Node) CompareGreaterEqual

func (n *Node) CompareGreaterEqual(v interface{}) interface{}

func (*Node) CompareLess

func (n *Node) CompareLess(v interface{}) interface{}

func (*Node) CompareLessEqual

func (n *Node) CompareLessEqual(v interface{}) interface{}

func (*Node) CompareMatch

func (n *Node) CompareMatch(v interface{}) interface{}

func (*Node) CompareNotEqual

func (n *Node) CompareNotEqual(v interface{}) interface{}

func (Node) String

func (n Node) String() string

func (*Node) ValueAdd

func (n *Node) ValueAdd(v interface{}) interface{}

func (*Node) ValueDiv

func (n *Node) ValueDiv(v interface{}) interface{}

func (*Node) ValueMul

func (n *Node) ValueMul(v interface{}) interface{}

func (*Node) ValueSub

func (n *Node) ValueSub(v interface{}) interface{}

type Operation

type Operation func(v interface{}) interface{}

Operation is a method that perform an operation (conditional or not)

type ProcessOptions

type ProcessOptions int
const (
	Default  ProcessOptions = 0
	Enhanced ProcessOptions = 1
	Collapse ProcessOptions = 2
	Flat     ProcessOptions = 4
)

type Processor

type Processor struct {
	*parser.BaseJsonpathListener

	Nodes []*Node
	// contains filtered or unexported fields
}

Processor defines the JsonPath grammar processor

func NewProcessor

func NewProcessor() *Processor

NewProcessor creates a new grammar Processor

func (*Processor) ExitDotExpr

func (p *Processor) ExitDotExpr(ctx *parser.DotExprContext)

ExitDotExpr is called when production dotExpr is exited.

func (*Processor) ExitFilterExpr

func (p *Processor) ExitFilterExpr(ctx *parser.FilterExprContext)

ExitFilterExpr is called when production filterExpr is exited.

func (*Processor) ExitItemsExpr

func (p *Processor) ExitItemsExpr(ctx *parser.ItemsExprContext)

ExitItemsExpr is called when production itemsExpr is exited.

func (*Processor) ExitNamesExpr

func (p *Processor) ExitNamesExpr(ctx *parser.NamesExprContext)

ExitNamesExpr is called when production namesExpr is exited.

func (*Processor) ExitRangeExpr

func (p *Processor) ExitRangeExpr(ctx *parser.RangeExprContext)

ExitRangeExpr is called when production rangeExpr is exited.

func (*Processor) ExitScriptExpr

func (p *Processor) ExitScriptExpr(ctx *parser.ScriptExprContext)

ExitScriptExpr is called when production scriptExpr is exited.

func (*Processor) ExitStarExpr

func (p *Processor) ExitStarExpr(ctx *parser.StarExprContext)

ExitStarExpr is called when production starExpr is exited.

func (*Processor) Parse

func (p *Processor) Parse(expr string) bool

Parse parses a JsonPath expression

func (*Processor) Process

func (p *Processor) Process(v interface{}, options ProcessOptions) interface{}

Process input object according to parsed JsonPath

func (*Processor) Reset

func (p *Processor) Reset()

Reset resets the processor state, making it ready to process another input

func (*Processor) VisitErrorNode

func (p *Processor) VisitErrorNode(node antlr.ErrorNode)

VisitErrorNode is called when there is an error

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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