jsonpath

package
v1.7.8 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 9 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package jsonpath is the small JSONPath of the HTTP client's extraction rules and checks (SPEC §10.33 "Extraction and checks"): an evaluator of our own over the values encoding/json decodes, standard library only. See Compile for the syntax it supports.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(data []byte) (any, error)

Decode reads a JSON document so that numbers keep their text (json.Number). It fails with JSONPathBadDocument when the data is not one JSON value.

Types

type Path

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

Path is a compiled expression.

func Compile

func Compile(expr string) (*Path, error)

Compile reads an expression. The subset it supports is exactly this:

$              the whole document (every expression starts with it)
.name          the member "name" of an object (letters, digits and any characters except . [ ] ' " * and white space)
['name']       the member "name"; "name" in double quotes works too; \\ \' \" \n \t \/ and \uXXXX are escapes
[n]            the element n of an array, counted from 0; [-1] is the last
[*] and .*     every member of an object (its keys in sorted order) or every element of an array
..name         the member "name" at any depth, the node itself included (..['name'], ..[n], ..[*] and ..* work the same way)

Not supported, and refused: filters ([?(...)]), slices ([1:3]), unions ([0,1]), script expressions and functions. Eval gives all the matches; an extraction rule takes the first.

func (*Path) Eval

func (p *Path) Eval(doc any) []any

Eval gives every value the path selects in the document (a value decoded by Decode, or by encoding/json into any), in the order of the document; the members of an object are visited in the order of their sorted names. A path that selects nothing gives none.

func (*Path) First

func (p *Path) First(doc any) (any, bool)

First gives the first value the path selects.

func (*Path) String

func (p *Path) String() string

String is the expression as it was written.

Jump to

Keyboard shortcuts

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