jsonpath

package module
v1.1.5 Latest Latest
Warning

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

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

README

JSONPath

Build Status Godoc

JSONPath is a complete implementation of http://goessner.net/articles/JsonPath/. JSONPath can be combined with a script language. In many web samples it's combined with javascript. This framework comes without a script language but can be easily extended with one. See example.

It is based on Gval and can be combined with the modular expression languages based on gval. So for script features like multiply, length, regex or many more take a look at the documentation in the GoDoc.

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/machship-mm/gval.

Example (Gval)
package main

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

	"github.com/machship-mm/gval"

	"github.com/machship-mm/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/machship-mm/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/machship-mm/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/machship-mm/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 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 added in v1.1.5

func PlaceholderExtension() gval.Language

PlaceholderExtension is the JSONPath Language with placeholder

Types

This section is empty.

Jump to

Keyboard shortcuts

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