jmespath

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: May 31, 2023 License: Apache-2.0 Imports: 3 Imported by: 3

README

go-jmespath - A JMESPath implementation in Go

GoDoc codecov Go Report Card

go-jmespath is a GO implementation of JMESPath, which is a query language for JSON. It will take a JSON document and transform it into another JSON document through a JMESPath expression.

Using go-jmespath is really easy. There's a single function you use, jmespath.Search:

> import "github.com/jmespath-community/go-jmespath"
>
> var jsondata = []byte(`{"foo": {"bar": {"baz": [0, 1, 2, 3, 4]}}}`) // your data
> var data interface{}
> err := json.Unmarshal(jsondata, &data)
> result, err := jmespath.Search("foo.bar.baz[2]", data)
result = 2

In the example we gave the Search function input data of {"foo": {"bar": {"baz": [0, 1, 2, 3, 4]}}} as well as the JMESPath expression foo.bar.baz[2], and the Search function evaluated the expression against the input data to produce the result 2.

The JMESPath language can do a lot more than select an element from a list. Here are a few more examples:

> var jsondata = []byte(`{"foo": {"bar": {"baz": [0, 1, 2, 3, 4]}}}`) // your data
> var data interface{}
> err := json.Unmarshal(jsondata, &data)
> result, err := jmespath.Search("foo.bar", data)
result = { "baz": [ 0, 1, 2, 3, 4 ] }


> var jsondata  = []byte(`{"foo": [{"first": "a", "last": "b"},
                           {"first": "c", "last": "d"}]}`) // your data
> var data interface{}
> err := json.Unmarshal(jsondata, &data)
> result, err := jmespath.Search({"foo[*].first", data)
result [ 'a', 'c' ]


> var jsondata = []byte(`{"foo": [{"age": 20}, {"age": 25},
                           {"age": 30}, {"age": 35},
                           {"age": 40}]}`) // your data
> var data interface{}
> err := json.Unmarshal(jsondata, &data)
> result, err := jmespath.Search("foo[?age > `30`]")
result = [ { age: 35 }, { age: 40 } ]

You can also pre-compile your query. This is usefull if you are going to run multiple searches with it:

> var jsondata = []byte(`{"foo": "bar"}`)
> var data interface{}
> err := json.Unmarshal(jsondata, &data)
> precompiled, err := Compile("foo")
> if err != nil{
>   // ... handle the error
> }
> result, err := precompiled.Search(data)
result = "bar"

More Resources

The example above only show a small amount of what a JMESPath expression can do. If you want to take a tour of the language, the best place to go is the JMESPath Tutorial.

One of the best things about JMESPath is that it is implemented in many different programming languages including python, ruby, php, lua, etc. To see a complete list of libraries, check out the JMESPath libraries page.

And finally, the full JMESPath specification can be found on the JMESPath site.

Documentation

Index

Constants

View Source
const (
	JpNumber      = functions.JpNumber
	JpString      = functions.JpString
	JpArray       = functions.JpArray
	JpObject      = functions.JpObject
	JpArrayArray  = functions.JpArrayArray
	JpArrayNumber = functions.JpArrayNumber
	JpArrayString = functions.JpArrayString
	JpExpref      = functions.JpExpref
	JpAny         = functions.JpAny
)

Variables

View Source
var (
	Compile     = api.Compile
	MustCompile = api.MustCompile
	Search      = api.Search
)
View Source
var NewParser = parsing.NewParser

Functions

This section is empty.

Types

type ArgSpec added in v1.1.1

type ArgSpec = functions.ArgSpec

type ExpRef added in v1.1.1

type ExpRef = functions.ExpRef

type FunctionEntry added in v1.1.1

type FunctionEntry = functions.FunctionEntry

type JMESPath

type JMESPath = api.JMESPath

type JpFunction added in v1.1.1

type JpFunction = functions.JpFunction

type JpType added in v1.1.1

type JpType = functions.JpType

type SyntaxError

type SyntaxError = parsing.SyntaxError

Directories

Path Synopsis
cmd
jpgo
Basic command line interface for debug and testing purposes.
Basic command line interface for debug and testing purposes.
pkg
api

Jump to

Keyboard shortcuts

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