jsonpath

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2020 License: MIT Imports: 7 Imported by: 9

README

JSONPath

Build Status Go Report Card Coverage Status Go Reference

This is Go package providing the features that retrieves a part of the JSON objects according to the statement written in the JSONPath syntax.

The core syntaxes of the JSONPath on which this package is based: JSONPath - XPath for JSON.

Note:

The unstated syntaxes found in "JSONPath - XPath for JSON" are implemented with reference to the test cases written in cburmer's json-path-comparison. Please check my compare result to know which responses are adapted. Unfortunately, the proposals that is also discussing in "json-path-comparison" were not finalized at the start of development and were not adopted outright.

Getting started

go get github.com/AsaiYusuke/jsonpath
Simple example:
package main

import (
  "encoding/json"
  "fmt"

  "github.com/AsaiYusuke/jsonpath"
)

func main() {
  jsonPath, srcJSON := `$.key`, `{"key":"value"}`
  var src interface{}
  json.Unmarshal([]byte(srcJSON), &src)
  output, _ := jsonpath.Retrieve(jsonPath, src)
  outputJSON, _ := json.Marshal(output)
  fmt.Println(string(outputJSON))
  // Output:
  // ["value"]
}

Basic design

  • PEG separated the JSONPath syntax analyzer from functionality itself to simplify the source.
  • The error specification allows package users to handle errors appropriately.
  • Adopted more of the consensus behavior from the cburmer's json-path-comparison. Adapted my own behavior to the other part of the such consensus behavior that found difficult to use.
  • Equipped with numerous unit tests and tried to eliminate the bugs that return strange result.

How to use

* Retrieve one-time, or successively

The Retrieve function returns a retrieved JSON object by a one-time sequential operation (analyzing syntax and retrieving objects) using the given JSONPath and the source JSON object :

output, err := jsonpath.Retrieve(jsonPath, src)

The Parse function returns a parser-function that completed to analyze the JSONPath syntax. By using this returned parser-function it can be performed successively a retrieve with the same JSONPath syntax :

jsonPath, err := jsonpath.Parse(jsonPath)
output1, err1 := jsonPath(src1)
output2, err2 := jsonPath(src2)
:
* Error handling

If there is a problem with the execution of the Retrieve, Prepare or prepared parser-functions, an error type is returned. These error types define the corresponding symptom, as listed below:

Syntax analyze errors from Retrieve, Parse
Error type Message format Symptom
ErrorInvalidSyntax invalid syntax (position=%d, reason=%s, near=%s) The invalid syntax found in the JSONPath. The reason including in this message will tell you more about it.
ErrorInvalidArgument invalid argument (argument=%s, error=%s) The argument specified in the JSONPath was treated as the invalid error in Go syntax.
ErrorFunctionNotFound function not found (function=%s) The function specified in the JSONPath is not found.
ErrorNotSupported not supported (feature=%s, path=%s) The unsupported syntaxes specified in the JSONPath.
Runtime errors from Retrieve, parser-functions
Error type Message format Symptom
ErrorMemberNotExist member did not exist (path=%s) The object member specified in the JSONPath did not exist in the JSON object.
ErrorIndexOutOfRange index out of range (path=%s) The array indexes specified in the JSONPath were out of range.
ErrorTypeUnmatched type unmatched (expected=%s, found=%s, path=%s) The node type specified in the JSONPath did not exist in the JSON object.
ErrorNoneMatched none matched (path=%s) The retrieving child paths specified in the JSONPath resulted in empty output.
ErrorFunctionFailed function failed (function=%s, error=%s) The function specified in the JSONPath failed.

The type checking is convenient to recognize which error happened.

  :
  _,err := jsonpath.Retrieve(jsonPath, srcJSON)
  if err != nil {
    switch err.(type) {
    case jsonpath.ErrorIndexOutOfRange:
      fmt.printf(`retry with other srcJSON: %v`, err)
      continue
    case jsonpath.ErrorInvalidArgumentFormat:
      return nil, fmt.errorf(`specified invalid argument: %v`, err)
    }
    :
  }
* Function syntax

Function is a feature that allows you to format JSONPath results by using pre-registered user functions and the instruction syntaxes at the end of the JSONPath statement.

There are two ways to use function:

Filter function

The filter function applies a user function to each values in the JSONPath result to get converted.

  config := jsonpath.Config{}
  config.SetFilterFunction(`twice`, func(param interface{}) (interface{}, error) {
    if floatParam, ok := param.(float64); ok {
      return floatParam * 2, nil
    }
    return nil, fmt.Errorf(`type error`)
  })
  jsonPath, srcJSON := `$[*].twice()`, `[1,3]`
  var src interface{}
  json.Unmarshal([]byte(srcJSON), &src)
  output, _ := jsonpath.Retrieve(jsonPath, src, config)
  outputJSON, _ := json.Marshal(output)
  fmt.Println(string(outputJSON))
  // Output:
  // [2,6]
Aggregate function

Aggregate function converts all values in the JSONPath result into a single value by applying them to a user function.

  config := jsonpath.Config{}
  config.SetAggregateFunction(`max`, func(params []interface{}) (interface{}, error) {
    var result float64
    for _, param := range params {
      if floatParam, ok := param.(float64); ok {
        if result < floatParam {
          result = floatParam
        }
        continue
      }
      return nil, fmt.Errorf(`type error`)
    }
    return result, nil
  })
  jsonPath, srcJSON := `$[*].max()`, `[1,3]`
  var src interface{}
  json.Unmarshal([]byte(srcJSON), &src)
  output, _ := jsonpath.Retrieve(jsonPath, src, config)
  outputJSON, _ := json.Marshal(output)
  fmt.Println(string(outputJSON))
  // Output:
  // [3]

Differences

Some behaviors that differ from the consensus exists in this package. For the entire comparisons, please check this result to see which responses are different. These behaviors will be changed in the future if appropriate ones are found.

Character types

The character types that can not be used for the identifiers in the dot notation are as follows :

. [ ( ) = ! > < \t \r \n *SPACE*

You have to encode these characters when you enter them :

JSONPath : $.abc\.def
srcJSON  : {"abc.def":1}
Output   : 1
Asterisk in qualifier

The asterisk in qualifier can be specified mixed with other subscript syntaxes.

JSONPath : $[0,1:3,*]
srcJSON  : [0,1,2,3,4,5]
Output   : [0,1,2,0,1,2,3,4,5]
Regular expression

The regular expression syntax works as a regular expression in Go lang. In particular, you can use "(?i)" to specify the regular expression as the ignore case option.

JSONPath : $[?(@.a=~/(?i)CASE/)]
srcJSON  : ["Case","Hello"]
Output   : ["Case"]
JSONPaths in the filter-qualifier

In the case of the comparators and regular expressions in the filter qualifier, the following JSONPaths that return a value group cannot be specified. On the other hand, in the case of the existence check in the filter qualifier, it can be specified.

JSONPaths that return a value group example
Recursive descent @..a
Multiple identifier @['a','b']
Asterisk identifier @.*
Slice qualifier @[0:1]
Asterisk qualifier @[*]
Union in the qualifier @[0,1]
Filter qualifier @.a[?(@.b)]

Benchmarks

Show results
goos: windows
goarch: amd64
pkg: github.com/AsaiYusuke/jsonpath
BenchmarkRetrieve_dotNotation-4                             	   11193	     97395 ns/op	  533524 B/op	     144 allocs/op
BenchmarkRetrieve_bracketNotation-4                         	    9456	    109682 ns/op	  533610 B/op	     147 allocs/op
BenchmarkRetrieve_asterisk_identifier_dotNotation-4         	   10000	    108195 ns/op	  533538 B/op	     145 allocs/op
BenchmarkRetrieve_asterisk_identifier_bracketNotation-4     	   10000	    105758 ns/op	  533594 B/op	     146 allocs/op
BenchmarkRetrieve_multi_identifier-4                        	   10000	    109516 ns/op	  533889 B/op	     157 allocs/op
BenchmarkRetrieve_qualifier_index-4                         	   10000	    108211 ns/op	  533601 B/op	     148 allocs/op
BenchmarkRetrieve_qualifier_slice-4                         	   10000	    120385 ns/op	  533808 B/op	     152 allocs/op
BenchmarkRetrieve_qualifier_asterisk-4                      	   10000	    110426 ns/op	  533561 B/op	     146 allocs/op
BenchmarkRetrieve_qualifier_union-4                         	    9457	    118281 ns/op	  533892 B/op	     159 allocs/op
BenchmarkRetrieve_filter_logicalOR-4                        	    9456	    133341 ns/op	  534582 B/op	     159 allocs/op
BenchmarkRetrieve_filter_logicalAND-4                       	    8197	    132956 ns/op	  534583 B/op	     159 allocs/op
BenchmarkRetrieve_filter_nodeFilter-4                       	   10000	    120922 ns/op	  534319 B/op	     158 allocs/op
BenchmarkRetrieve_filter_logicalNOT-4                       	    9463	    130181 ns/op	  534389 B/op	     161 allocs/op
BenchmarkRetrieve_filter_compareEQ-4                        	    8197	    143824 ns/op	  534771 B/op	     166 allocs/op
BenchmarkRetrieve_filter_compareNE-4                        	    7681	    151789 ns/op	  534787 B/op	     167 allocs/op
BenchmarkRetrieve_filter_compareGE-4                        	    8194	    143069 ns/op	  534768 B/op	     166 allocs/op
BenchmarkRetrieve_filter_compareGT-4                        	    8782	    150355 ns/op	  534661 B/op	     165 allocs/op
BenchmarkRetrieve_filter_compareLE-4                        	    8778	    142800 ns/op	  534754 B/op	     165 allocs/op
BenchmarkRetrieve_filter_compareLT-4                        	    9458	    142593 ns/op	  534656 B/op	     164 allocs/op
BenchmarkRetrieve_filter_regex-4                            	    8781	    147454 ns/op	  543052 B/op	     178 allocs/op
BenchmarkParserFunc_dotNotation-4                           	 7589292	       163 ns/op	      48 B/op	       2 allocs/op
BenchmarkParserFunc_bracketNotation-4                       	 8721170	       136 ns/op	      48 B/op	       2 allocs/op
BenchmarkParserFunc_asterisk_identifier_dotNotation-4       	 4210114	       281 ns/op	      96 B/op	       4 allocs/op
BenchmarkParserFunc_asterisk_identifier_bracketNotation-4   	 3898969	       297 ns/op	      96 B/op	       4 allocs/op
BenchmarkParserFunc_multi_identifier-4                      	 4917340	       248 ns/op	      80 B/op	       3 allocs/op
BenchmarkParserFunc_qualifier_index-4                       	 6505556	       189 ns/op	      64 B/op	       4 allocs/op
BenchmarkParserFunc_qualifier_slice-4                       	 5469730	       225 ns/op	      64 B/op	       4 allocs/op
BenchmarkParserFunc_qualifier_asterisk-4                    	 6151656	       194 ns/op	      64 B/op	       4 allocs/op
BenchmarkParserFunc_qualifier_union-4                       	 3419431	       358 ns/op	     120 B/op	       7 allocs/op
BenchmarkParserFunc_filter_logicalOR-4                      	 1000000	      1181 ns/op	     912 B/op	      12 allocs/op
BenchmarkParserFunc_filter_logicalAND-4                     	  945879	      1193 ns/op	     912 B/op	      12 allocs/op
BenchmarkParserFunc_filter_nodeFilter-4                     	 1575550	       751 ns/op	     608 B/op	       8 allocs/op
BenchmarkParserFunc_filter_logicalNOT-4                     	 1215165	      1000 ns/op	     656 B/op	      10 allocs/op
BenchmarkParserFunc_filter_compareEQ-4                      	  876064	      1363 ns/op	     864 B/op	      10 allocs/op
BenchmarkParserFunc_filter_compareNE-4                      	  875866	      1418 ns/op	     864 B/op	      10 allocs/op
BenchmarkParserFunc_filter_compareGE-4                      	  942987	      1279 ns/op	     864 B/op	      10 allocs/op
BenchmarkParserFunc_filter_compareGT-4                      	  942847	      1267 ns/op	     864 B/op	      10 allocs/op
BenchmarkParserFunc_filter_compareLE-4                      	  945812	      1267 ns/op	     864 B/op	      10 allocs/op
BenchmarkParserFunc_filter_compareLT-4                      	  942994	      1274 ns/op	     864 B/op	      10 allocs/op
BenchmarkParserFunc_filter_regex-4                          	  862663	      1411 ns/op	     871 B/op	      10 allocs/op

Project progress

  • Syntax
    • Identifier
      • identifier in dot notations
      • identifier in bracket notations
      • asterisk identifier
      • multiple-identifier in bracket
      • recursive retrieve
    • Qualifier
      • index
      • slice
      • asterisk
      • Filter
        • logical operation
        • comparator
        • JSONPath retrieve in filter
      • script
    • Function
      • filter
      • aggregate
    • Refer to the consensus behaviors
  • Archtecture
    • PEG syntax analyzing
    • Error handling
  • Go language manner
    • retrieve with the object in interface unmarshal
    • retrieve with the json.Number type
  • Source code
    • Release version
    • Unit tests
      • syntax tests
      • benchmark
      • coverage >80%
    • Examples
    • CI automation
    • Documentation
      • README
      • API doc
    • comparison result (local)
  • Development status
    • determine requirements / functional design
    • design-based coding
    • testing
    • documentation
  • Future ToDo
    • Refer to the something standard
    • Go language affinity
      • retrieve with the object in struct unmarshal
      • retrieve with the struct tags
      • retrieve with the user defined objects

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(jsonPath string, config ...Config) (func(src interface{}) ([]interface{}, error), error)

Parse returns the parser function using the given JSONPath.

Example
package main

import (
	"encoding/json"
	"fmt"
	"reflect"

	"github.com/AsaiYusuke/jsonpath"
)

func main() {
	jsonPath := `$.key`
	srcJSON1 := `{"key":"value1"}`
	srcJSON2 := `{"key":"value2"}`
	jsonPathParser, err := jsonpath.Parse(jsonPath)
	if err != nil {
		fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
		return
	}
	var src1, src2 interface{}
	json.Unmarshal([]byte(srcJSON1), &src1)
	json.Unmarshal([]byte(srcJSON2), &src2)
	output1, err := jsonPathParser(src1)
	if err != nil {
		fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
		return
	}
	output2, err := jsonPathParser(src2)
	if err != nil {
		fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
		return
	}
	outputJSON1, _ := json.Marshal(output1)
	outputJSON2, _ := json.Marshal(output2)
	fmt.Println(string(outputJSON1))
	fmt.Println(string(outputJSON2))
}
Output:

["value1"]
["value2"]

func Retrieve

func Retrieve(jsonPath string, src interface{}, config ...Config) ([]interface{}, error)

Retrieve returns the retrieved JSON using the given JSONPath.

Example
package main

import (
	"encoding/json"
	"fmt"
	"reflect"

	"github.com/AsaiYusuke/jsonpath"
)

func main() {
	jsonPath, srcJSON := `$.key`, `{"key":"value"}`
	var src interface{}
	json.Unmarshal([]byte(srcJSON), &src)
	output, err := jsonpath.Retrieve(jsonPath, src)
	if err != nil {
		fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
		return
	}
	outputJSON, _ := json.Marshal(output)
	fmt.Println(string(outputJSON))
}
Output:

["value"]

Types

type Config added in v1.1.0

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

Config represents the configuration parameters.

func (*Config) SetAggregateFunction added in v1.1.0

func (c *Config) SetAggregateFunction(id string, function func([]interface{}) (interface{}, error))

SetAggregateFunction set the custom function.

Example
package main

import (
	"encoding/json"
	"fmt"
	"reflect"

	"github.com/AsaiYusuke/jsonpath"
)

func main() {
	config := jsonpath.Config{}
	config.SetAggregateFunction(`max`, func(params []interface{}) (interface{}, error) {
		var result float64
		for _, param := range params {
			if floatParam, ok := param.(float64); ok {
				if result < floatParam {
					result = floatParam
				}
				continue
			}
			return nil, fmt.Errorf(`type error`)
		}
		return result, nil
	})
	jsonPath, srcJSON := `$[*].max()`, `[1,3]`
	var src interface{}
	json.Unmarshal([]byte(srcJSON), &src)
	output, err := jsonpath.Retrieve(jsonPath, src, config)
	if err != nil {
		fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
		return
	}
	outputJSON, _ := json.Marshal(output)
	fmt.Println(string(outputJSON))
}
Output:

[3]

func (*Config) SetFilterFunction added in v1.1.0

func (c *Config) SetFilterFunction(id string, function func(interface{}) (interface{}, error))

SetFilterFunction set the custom function.

Example
package main

import (
	"encoding/json"
	"fmt"
	"reflect"

	"github.com/AsaiYusuke/jsonpath"
)

func main() {
	config := jsonpath.Config{}
	config.SetFilterFunction(`twice`, func(param interface{}) (interface{}, error) {
		if floatParam, ok := param.(float64); ok {
			return floatParam * 2, nil
		}
		return nil, fmt.Errorf(`type error`)
	})
	jsonPath, srcJSON := `$[*].twice()`, `[1,3]`
	var src interface{}
	json.Unmarshal([]byte(srcJSON), &src)
	output, err := jsonpath.Retrieve(jsonPath, src, config)
	if err != nil {
		fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
		return
	}
	outputJSON, _ := json.Marshal(output)
	fmt.Println(string(outputJSON))
}
Output:

[2,6]

type ErrorFunctionFailed added in v1.1.0

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

ErrorFunctionFailed represents the error that the function specified in the JSONPath failed.

Example
package main

import (
	"encoding/json"
	"fmt"
	"reflect"

	"github.com/AsaiYusuke/jsonpath"
)

func main() {
	config := jsonpath.Config{}
	config.SetFilterFunction(`invalid`, func(param interface{}) (interface{}, error) {
		return nil, fmt.Errorf(`invalid function executed`)
	})
	jsonPath, srcJSON := `$.invalid()`, `{}`
	var src interface{}
	json.Unmarshal([]byte(srcJSON), &src)
	output, err := jsonpath.Retrieve(jsonPath, src, config)
	if err != nil {
		fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
		return
	}
	outputJSON, _ := json.Marshal(output)
	fmt.Println(string(outputJSON))
}
Output:

jsonpath.ErrorFunctionFailed, function failed (function=.invalid(), error=invalid function executed)

func (ErrorFunctionFailed) Error added in v1.1.0

func (e ErrorFunctionFailed) Error() string

type ErrorFunctionNotFound added in v1.1.0

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

ErrorFunctionNotFound represents the error that the function specified in the JSONPath is not found.

Example
package main

import (
	"encoding/json"
	"fmt"
	"reflect"

	"github.com/AsaiYusuke/jsonpath"
)

func main() {
	jsonPath, srcJSON := `$.unknown()`, `{}`
	var src interface{}
	json.Unmarshal([]byte(srcJSON), &src)
	output, err := jsonpath.Retrieve(jsonPath, src)
	if err != nil {
		fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
		return
	}
	outputJSON, _ := json.Marshal(output)
	fmt.Println(string(outputJSON))
}
Output:

jsonpath.ErrorFunctionNotFound, function not found (function=.unknown())

func (ErrorFunctionNotFound) Error added in v1.1.0

func (e ErrorFunctionNotFound) Error() string

type ErrorIndexOutOfRange

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

ErrorIndexOutOfRange represents the error that the array indexes specified in the JSONPath are out of range.

Example
package main

import (
	"encoding/json"
	"fmt"
	"reflect"

	"github.com/AsaiYusuke/jsonpath"
)

func main() {
	jsonPath, srcJSON := `$[1]`, `["a"]`
	var src interface{}
	json.Unmarshal([]byte(srcJSON), &src)
	output, err := jsonpath.Retrieve(jsonPath, src)
	if err != nil {
		fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
		return
	}
	outputJSON, _ := json.Marshal(output)
	fmt.Println(string(outputJSON))
}
Output:

jsonpath.ErrorIndexOutOfRange, index out of range (path=[1])

func (ErrorIndexOutOfRange) Error

func (e ErrorIndexOutOfRange) Error() string

type ErrorInvalidArgument

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

ErrorInvalidArgument represents the error that argument specified in the JSONPath is treated as the invalid error in Go syntax.

Example
package main

import (
	"encoding/json"
	"fmt"
	"reflect"

	"github.com/AsaiYusuke/jsonpath"
)

func main() {
	jsonPath, srcJSON := `$[?(1.0.0>0)]`, `{}`
	var src interface{}
	json.Unmarshal([]byte(srcJSON), &src)
	output, err := jsonpath.Retrieve(jsonPath, src)
	if err != nil {
		fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
		return
	}
	outputJSON, _ := json.Marshal(output)
	fmt.Println(string(outputJSON))
}
Output:

jsonpath.ErrorInvalidArgument, invalid argument (argument=1.0.0, error=strconv.ParseFloat: parsing "1.0.0": invalid syntax)

func (ErrorInvalidArgument) Error

func (e ErrorInvalidArgument) Error() string

type ErrorInvalidSyntax

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

ErrorInvalidSyntax represents the error that have syntax error in the JSONPath.

Example
package main

import (
	"encoding/json"
	"fmt"
	"reflect"

	"github.com/AsaiYusuke/jsonpath"
)

func main() {
	jsonPath, srcJSON := `$.`, `{}`
	var src interface{}
	json.Unmarshal([]byte(srcJSON), &src)
	output, err := jsonpath.Retrieve(jsonPath, src)
	if err != nil {
		fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
		return
	}
	outputJSON, _ := json.Marshal(output)
	fmt.Println(string(outputJSON))
}
Output:

jsonpath.ErrorInvalidSyntax, invalid syntax (position=1, reason=unrecognized input, near=.)

func (ErrorInvalidSyntax) Error

func (e ErrorInvalidSyntax) Error() string

type ErrorMemberNotExist

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

ErrorMemberNotExist represents the error that the object member specified in the JSONPath did not exist in the JSON object.

Example
package main

import (
	"encoding/json"
	"fmt"
	"reflect"

	"github.com/AsaiYusuke/jsonpath"
)

func main() {
	jsonPath, srcJSON := `$.none`, `{}`
	var src interface{}
	json.Unmarshal([]byte(srcJSON), &src)
	output, err := jsonpath.Retrieve(jsonPath, src)
	if err != nil {
		fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
		return
	}
	outputJSON, _ := json.Marshal(output)
	fmt.Println(string(outputJSON))
}
Output:

jsonpath.ErrorMemberNotExist, member did not exist (path=.none)

func (ErrorMemberNotExist) Error

func (e ErrorMemberNotExist) Error() string

type ErrorNoneMatched

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

ErrorNoneMatched represents the error that the child paths specified in the JSONPath result in empty output.

Example
package main

import (
	"encoding/json"
	"fmt"
	"reflect"

	"github.com/AsaiYusuke/jsonpath"
)

func main() {
	jsonPath, srcJSON := `$[1,2]`, `["a"]`
	var src interface{}
	json.Unmarshal([]byte(srcJSON), &src)
	output, err := jsonpath.Retrieve(jsonPath, src)
	if err != nil {
		fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
		return
	}
	outputJSON, _ := json.Marshal(output)
	fmt.Println(string(outputJSON))
}
Output:

jsonpath.ErrorNoneMatched, none matched (path=[1,2])

func (ErrorNoneMatched) Error

func (e ErrorNoneMatched) Error() string

type ErrorNotSupported

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

ErrorNotSupported represents the error that the unsupported syntaxes specified in the JSONPath.

Example
package main

import (
	"encoding/json"
	"fmt"
	"reflect"

	"github.com/AsaiYusuke/jsonpath"
)

func main() {
	jsonPath, srcJSON := `$[(command)]`, `{}`
	var src interface{}
	json.Unmarshal([]byte(srcJSON), &src)
	output, err := jsonpath.Retrieve(jsonPath, src)
	if err != nil {
		fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
		return
	}
	outputJSON, _ := json.Marshal(output)
	fmt.Println(string(outputJSON))
}
Output:

jsonpath.ErrorNotSupported, not supported (feature=script, path=[(command)])

func (ErrorNotSupported) Error

func (e ErrorNotSupported) Error() string

type ErrorTypeUnmatched

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

ErrorTypeUnmatched represents the error that the node type specified in the JSONPath did not exist in the JSON object.

Example
package main

import (
	"encoding/json"
	"fmt"
	"reflect"

	"github.com/AsaiYusuke/jsonpath"
)

func main() {
	jsonPath, srcJSON := `$.a`, `[]`
	var src interface{}
	json.Unmarshal([]byte(srcJSON), &src)
	output, err := jsonpath.Retrieve(jsonPath, src)
	if err != nil {
		fmt.Printf(`%v, %v`, reflect.TypeOf(err), err)
		return
	}
	outputJSON, _ := json.Marshal(output)
	fmt.Println(string(outputJSON))
}
Output:

jsonpath.ErrorTypeUnmatched, type unmatched (expected=object, found=[]interface {}, path=.a)

func (ErrorTypeUnmatched) Error

func (e ErrorTypeUnmatched) Error() string

Source Files

Jump to

Keyboard shortcuts

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