jsonpath

package module
v0.0.0-...-c2be9c4 Latest Latest
Warning

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

Go to latest
Published: May 4, 2023 License: MIT Imports: 8 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.

this library is till bleeding edge, so use it at your own risk. :D

Golang Version Required: 1.5+

Get Started

go get github.com/larksuite/jsonpath

example code:

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

var json_data interface{}
json.Unmarshal([]byte(data), &json_data)

res, err := jsonpath.JsonPathLookup(json_data, "$.expensive")

//or reuse lookup pattern
pat, _ := jsonpath.Compile(`$.store.book[?(@.price < $.expensive)].price`)
res, err := pat.Lookup(json_data)

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.
* X Wildcard. Available anywhere a name or numeric are required.
.. X Deep scan. Available anywhere a name is required.
.<name> Y Dot-notated child
['<name>' (, '<name>')] X Bracket-notated child or children
[<number> (, <number>)] Y Array index or indexes
[start:end] Y Array slice operator
[?(<expression>)] 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 && @.author == 'Evelyn Waugh')].title ["Sword of Honour"]
$.store.book[?(@.price < $.expensive)].price [8.95, 8.99]
$.store.book[:].price [8.9.5, 12.99, 8.9.9, 22.99]
$.store.book[?(@.author =~ /(?i).*REES/)].author "Nigel Rees"

Note: golang support regular expression flags in form of (?imsU)pattern

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrGetFromNullObj = errors.New("get attribute from null object")
View Source
var IsNull = errors.New("object is nil")
View Source
var NotJSON = errors.New("object is not json")
View Source
var NotMap = errors.New("object is not map")
View Source
var NotSlice = errors.New("object is not slice")

Functions

func Set

func Set(obj interface{}, jpath string, val interface{}) error

func TranslatePath

func TranslatePath(obj interface{}, path string) (string, error)

Types

type Compiled

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

func Compile

func Compile(path string) (*Compiled, error)

func MustCompile

func MustCompile(jpath string) *Compiled

func (*Compiled) Lookup

func (c *Compiled) Lookup(obj interface{}) (res interface{}, isArray bool, err error)

func (*Compiled) Set

func (c *Compiled) Set(obj interface{}, val interface{}) error

func (*Compiled) String

func (c *Compiled) String() string

type FilterExpression

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

type Result

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

func Get

func Get(obj interface{}, path string) (*Result, error)

func (*Result) First

func (r *Result) First() interface{}

First Provides the first item of an array

func (*Result) Value

func (r *Result) Value() interface{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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