Skip to content
forked from blues/jsonata-go

About

Go port of JSONata

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

 
 

Latest commit

 

History

68 Commits

Folders and files

Repository files navigation

JSONata in Go (jsonata-go)

This repository is an updated fork of the original blues/jsonata-go library, upgraded to support JSONata 2.2.2 specifications and WebAssembly

Package jsonata is a query and transformation language for JSON data implemented in pure Go. It is a high-performance port of the reference JavaScript implementation, JSONata.

This repository maintains 100% feature compliance with jsonata-js v2.2.2, passing all 1,700+ upstream test cases with zero skips or failures.


Features

  • Full JSONata 2.2.2 Parity: Complete support for path navigation, wildcards, descendants, predicates, sorting, grouping, lambdas, closures, transforms, regex, and the 50+ standard library functions.
  • Pure Go: Zero external runtime dependencies.
  • WebAssembly (WASM) Support: Ready-to-build WASM entry point for in-browser evaluation and UI playgrounds with panic recovery and AST caching.
  • Included Utilities:
  • jsonata-server: A locally hosted JSONata Exerciser web application.
  • jsonata-test: CLI runner to evaluate the engine directly against the official jsonata-js test suite.

Installation

Requires Go 1.27+.

go get github.com/darius-lesch/jsonata-go/v2

Quick Start

package main

import (
	jsonv2 "encoding/json/v2"
	"fmt"
	"log"

	jsonata "github.com/darius-lesch/jsonata-go/v2"
)

const jsonString = `
{
    "orders": [
        {"price": 10, "quantity": 3},
        {"price": 0.5, "quantity": 10},
        {"price": 100, "quantity": 1}
    ]
}
`

func main() {
	var data any

	// Decode JSON
	if err := jsonv2.Unmarshal([]byte(jsonString), &data); err != nil {
		log.Fatal(err)
	}

	// Compile JSONata expression
	expr, err := jsonata.Compile("$sum(orders.(price * quantity))")
	if err != nil {
		log.Fatal(err)
	}

	// Evaluate against data payload
	res, err := expr.Eval(data)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(res)
	// Output: 135
}

WebAssembly (WASM) Support

jsonata-go includes a WebAssembly entry point inside ./wasm/ designed for client-side browser evaluation.

Building the WASM Binary

GOOS=js GOARCH=wasm go build -ldflags="-s -w" -trimpath -o jsonata.wasm ./wasm/

Browser Usage

The compiled WASM module exposes the _jsonataGoEval global function on window:

// _jsonataGoEval(expressionString, jsonDataString) -> string | Error
try {
  const resultJson = window._jsonataGoEval(
    "$sum(orders.(price * quantity))",
    JSON.stringify(payload)
  );
  console.log(JSON.parse(resultJson)); // 135
} catch (err) {
  console.error("Evaluation Error:", err.message);
}

Included Tools & Utilities

1. JSONata Server (jsonata-server)

A locally hosted version of the JSONata Exerciser web UI for interactively testing expressions against live JSON payloads. Static assets (HTML, CSS, JavaScript, and CodeMirror editor) are embedded directly into the binary, allowing jsonata-server to be executed from any working directory without external file dependencies.

Installation & Running

# Install the binary locally:
go install ./jsonata-server

# Or install the latest release remotely:
# go install github.com/darius-lesch/jsonata-go/v2/jsonata-server@latest

# Launch the server (default port 8080):
jsonata-server -port=8080

# Or run directly without installation:
go run ./jsonata-server -port=8080

Navigate to http://localhost:8080 in your browser to use the interactive exerciser.

CLI Flags

Flag Default Description
-port 8080 The port number to serve HTTP traffic on.
-site "" Optional path to a custom directory to serve static files from (overriding embedded assets; useful for frontend development).

HTTP Endpoints

  • GET /: Serves the interactive JSONata Exerciser web application.
  • POST /eval: Evaluates a JSONata expression against a JSON payload. Expects application/x-www-form-urlencoded or multipart form data:
    • expr: The JSONata query/expression string.
    • json: The JSON input document string.
  • GET /bench: Executes a predefined benchmark expression against sample telemetry data and returns "success".

2. Upstream Test Suite Runner (jsonata-test)

A CLI utility used to run jsonata-go directly against the official jsonata-js test-suite.

Installation & Execution

  1. Install the CLI runner:
go install github.com/darius-lesch/jsonata-go/v2/jsonata-test@latest
  1. Clone the official jsonata-js repository:
git clone https://github.com/jsonata-js/jsonata.git
cd jsonata
git checkout v2.2.2
  1. Execute the test runner against the suite:
jsonata-test ./test/test-suite

Contributing

Contributions, bug reports, and pull requests are welcome! Prior to submitting a PR, please ensure all unit tests pass and code is formatted cleanly:

go test ./...
go fmt ./...

About

Go port of JSONata

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages