jsonpath

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

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

Go to latest
Published: Oct 8, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

README

jsonpath

PkgGoDev Build Status codecov

Go implementation of jsonpath

This is still a work in progress, updates to come.

Example

const jsonString = `{
  "firstName": "John",
  "lastName" : "doe",
  "age"      : 26,
  "address"  : {
    "streetAddress": "naist street",
    "city"         : "Nara",
    "postalCode"   : "630-0192"
  },
  "phoneNumbers": [
    {
      "type"  : "iPhone",
      "number": "0123-4567-8888"
    },
    {
      "type"  : "home",
      "number": "0123-4567-8910"
    },
    {
      "type"  : "mobile",
      "number": "0913-8532-8492"
    }
  ]
}`

result, err := Jsonpath([]byte(jsonString), "$.phoneNumbers[*].type")
if err != nil {
    log.Fatal(err)
}

for _, item := range result {
    fmt.Println(item)
}
// Output:
// iPhone
// home
// mobile

Supported operations

There are still a few operations which this library does not support but the goal is to support all of these path operations.

Operation Supported Description
$ Yes The root object/element.
@ No The current object/element. (To be added).
. or [] Yes Child operator. Within [] single quotes or double quotes can be used.
.. Yes Recursive decent.
* Yes Wildcard. All objects/elements regardless of their names.
[] Yes subscript operator. XPath uses it to iterate over element collections and for predicates. In Javascript and JSON it is the native array operator.
[,] Yes Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set.
[start:end:step] No Array slice operator borrowed from ES4. (To be added).
?() No Applies a filter (script) expression. (To be added).
() No Script expression, using the underlying script engine. (To be added).

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Jsonpath

func Jsonpath(data []byte, path string) ([]interface{}, error)

Jsonpath will evaluate the provided jsonpath on the provided json. It does not cache either of these parameters. If you are using the same jsonpath consistently then you would want to create an Evaluator for that path instead. An error is returned if there is a problem parsing the jsonpath or if the json could not be parsed.

Example

ExampleJsonpath is a simple example on how to extract a single field from each object in an array using jsonpath.

const jsonString = `{
	  "firstName": "John",
	  "lastName" : "doe",
	  "age"      : 26,
	  "address"  : {
		"streetAddress": "naist street",
		"city"         : "Nara",
		"postalCode"   : "630-0192"
	  },
	  "phoneNumbers": [
		{
		  "type"  : "iPhone",
		  "number": "0123-4567-8888"
		},
		{
		  "type"  : "home",
		  "number": "0123-4567-8910"
		},
		{
		  "type": "mobile",
		  "number": "0913-8532-8492"
		}
	  ]
	}`

result, err := Jsonpath([]byte(jsonString), "$.phoneNumbers[*].type")
if err != nil {
	log.Fatal(err)
}

for _, item := range result {
	fmt.Println(item)
}
Output:

iPhone
home
mobile

Types

type Evaluator

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

Evaluator is a compiled form of a jsonpath. It can run it's jsonpath against any provided json object and only needs to parse the provided json.

func NewEvaluator

func NewEvaluator(path string) (*Evaluator, error)

NewEvaluator will compile the provided jsonpath and create an object that can run that expression on provided json objects. If the path is not valid then an error is returned.

func (*Evaluator) Evaluate

func (e *Evaluator) Evaluate(data []byte) ([]interface{}, error)

Evaluate will run the compiled jsonpath against the provided json. It will return an array of objects that is the result of the expression or an error if something failed to evaluate or if the json was invalid.

Jump to

Keyboard shortcuts

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