The Go port of jqlite is a library that runs jq programs on JSON values from Go code. It is a
file-by-file port of the .NET reference engine: it follows the same specification
and passes the same conformance corpus (spec/tests). The package jqlite is the host API; the
engine lives in internal/ and is not importable.
go directive of go/go.mod).The module path is github.com/devlooped/jqlite/go and the package name is jqlite:
import "github.com/devlooped/jqlite/go"
The repository has no release tags for the Go module (a module in the go/ folder would need tags
of the form go/vX.Y.Z). Use it from a clone of the repository with a replace directive in your
own go.mod:
$ git clone https://github.com/devlooped/jqlite
$ cd myapp
$ go mod edit -require=github.com/devlooped/jqlite/go@v0.0.0 -replace=github.com/devlooped/jqlite/go=../jqlite/go
The embedded prelude (go/internal/binding/prelude.jq) is committed, so no go generate step is
needed to build against the clone. How to build and test the port itself: the port's README.
package main
import (
"fmt"
"log"
"github.com/devlooped/jqlite/go"
)
func main() {
input, err := jqlite.ParseJSON(`{"items":[{"name":"apple","price":5},{"name":"pear","price":15}]}`)
if err != nil {
log.Fatal(err)
}
results, err := jqlite.Evaluate(`.items[] | select(.price > 10) | {name, price}`, input, nil)
if err != nil {
log.Fatal(err)
}
for _, result := range results {
fmt.Println(jqlite.ToJSON(result))
}
}
{"name":"pear","price":15}
ParseJSON turns JSON text into a jqlite.Value. Evaluate parses the program, runs it on the
input and returns every result. The third argument holds the host variables (nil for none).
ToJSON writes a result as compact JSON text.
A program can give zero, one or many results, so results always come as a slice or a stream. To run
the same program many times, parse it once with Parse and keep the *jqlite.Expression: see
Evaluating.
| Page | What it covers |
|---|---|
| Evaluating | One-shot evaluation, parse once and evaluate many times, streaming, concurrency |
| Values | jqlite.Value: building inputs, reading results, numbers, strings, key order |
| Variables | Host variables ($name), $ENV, $__loc__ |
| Options | Environment, clock and time zone, depth limit, debug and stderr handlers |
| Modules | import, include, data modules, modulemeta and resolvers |
| Errors | ParseError, Error, HaltError, VariableError, and try in programs |
| JSON Lines | Evaluating a program on each value of a JSON Lines stream |
| API reference | Every exported identifier of the package |
| The port | Build, tests, layout and differences from the reference |
The language itself is described in the language guide; the differences from jq 1.8 are listed in the spec.