jsonpath

package module
v0.0.0-...-5cc68e5 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2018 License: BSD-3-Clause Imports: 6 Imported by: 80

README

Build Status

This was mostly an experiment to learn go and test using closures to interpret a JSON path. You should use https://github.com/PaesslerAG/jsonpath instead.

jsonpath

a (partial) implementation in Go based on Stefan Goener JSON Path

Limitations

  • No support for subexpressions : $books[(@.length-1)]
  • No support for filters : $books[?(@.price > 10)]
  • Strings in brackets must use double quotes : $["bookstore"]
  • Cannot operate on struct fields

The third limitation comes from using the text/scanner package from the standard library. The last one could be overcome by using reflection.

JsonPath quick intro

All expressions start $.

Examples (supported by the current implementation) :

  • $ the current object (or array)
  • $.books access to the key of an object (or $["books"]with bracket syntax)
  • $.books[1] access to the index of an array
  • $.books[1].authors[1].name chaining of keys and index
  • $["books"][1]["authors"][1]["name"] the same with braket syntax
  • $.books[0,1,3] union on an array
  • $["books", "songs", "movies"] union on an object
  • $books[1:3] second and third items of an array
  • $books[:-2:2] every two items except the last two of an array
  • $books[::-1] all items in reversed order
  • $..authors all authors (recursive search)

Checkout the tests for more examples.

Install

go get github.com/yalp/jsonpath

Usage

A jsonpath applies to any JSON decoded data using interface{} when decoded with 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.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

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.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/yalp/jsonpath"
)

func main() {
	raw := []byte(`{"hello":"world"}`)

	var data interface{}
	json.Unmarshal(raw, &data)

	out, err := jsonpath.Read(data, "$.hello")
	if err != nil {
		panic(err)
	}

	fmt.Print(out)
}
Output:

world

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.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/yalp/jsonpath"
)

func main() {
	raw := []byte(`{"hello":"world"}`)

	helloFilter, err := jsonpath.Prepare("$.hello")
	if err != nil {
		panic(err)
	}

	var data interface{}
	if err = json.Unmarshal(raw, &data); err != nil {
		panic(err)
	}

	out, err := helloFilter(data)
	if err != nil {
		panic(err)
	}

	fmt.Print(out)
}
Output:

world

Jump to

Keyboard shortcuts

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