Package jsonata is a query and transformation language for JSON. It's a Go port of the JavaScript library JSONata.
It currently has feature parity with jsonata-js 1.5.4. As well as a most of the functions added in newer versions. You can see potentially missing functions by looking at the jsonata-js changelog.
go get github.com/jsonata-go/jsonata
This library provides a unified interface for managing multiple versions of JSONata implementations, similar to how the JavaScript JSONata Exerciser allows users to select different versions.
JSONataInstance
: Interface representing a specific JSONata versionExpression
: Interface for compiled JSONata expressions
AvailableVersions() []string
: Returns sorted list of available versionsOpen(version string) (JSONataInstance, error)
: Opens a specific versionOpenLatest() (JSONataInstance, error)
: Opens the latest versionLatestVersion() string
: Returns the latest version string
import (
"fmt"
"log"
"github.com/jsonata-go/jsonata"
)
const jsonString = `
{
"orders": [
{"price": 10, "quantity": 3},
{"price": 0.5, "quantity": 10},
{"price": 100, "quantity": 1}
]
}
`
func main() {
// Open a specific version
instance, err := jsonata.Open("v2.0.6")
if err != nil {
log.Fatal(err)
}
// Create expression.
e, err := instance.Compile("$sum(orders.(price*quantity))", false)
if err != nil {
log.Fatal(err)
}
// Evaluate.
resultJson, err := e.Evaluate([]byte(jsonString), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(resultJson))
// Output: 135
}
import (
"fmt"
"log"
jsonata "github.com/jsonata-go/jsonata/v206"
)
const jsonString = `
{
"orders": [
{"price": 10, "quantity": 3},
{"price": 0.5, "quantity": 10},
{"price": 100, "quantity": 1}
]
}
`
func main() {
// Create expression.
e, err := jsonata.Compile("$sum(orders.(price*quantity))", false)
if err != nil {
log.Fatal(err)
}
// Evaluate.
resultJson, err := e.Evaluate([]byte(jsonString), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(resultJson))
// Output: 135
}
For detailed API documentation of the v206 implementation, including all available functions, methods, error handling, and security best practices, see the v206 API Documentation.
We love issues, fixes, and pull requests from everyone. Please run the unit-tests, vet, and staticcheck prior to submitting your PR. By participating in this project, you agree to abide by the Blues Inc code of conduct.
For details on contributions we accept and the process for contributing, see our contribution guide.