Go to file
2025-06-19 15:46:07 +01:00
.github/workflows chore: fix release action to avoid creating two releases 2025-05-07 16:12:02 +01:00
ast chore: update rules_go and gazelle version to fix bazel build (#756) 2024-06-10 21:24:27 +01:00
astgen release: prepare to release v0.21.0 2025-05-07 16:12:02 +01:00
bazel release: prepare to release v0.21.0 2025-05-07 16:12:02 +01:00
builtin-benchmarks feat: Go implementation for manifestYamlDoc and escapeStringJson (#742) 2024-06-09 19:27:15 +01:00
c-bindings Remove dependency on cpp-jsonnet submodule from C bindings 2025-03-31 11:25:18 +01:00
c-bindings-tests tests: fix python compat test to allow pre-release versions 2025-02-22 17:57:53 +00:00
cmd chore: remove refs to deprecated io/ioutil (#716) 2024-06-10 21:27:07 +01:00
cpp-jsonnet@bbb38f1020 release: prepare to release v0.21.0 2025-05-07 16:12:02 +01:00
examples/bazel release: prepare to release v0.21.0 2025-05-07 16:12:02 +01:00
formatter Use default options for formatter tests 2025-03-06 23:48:39 +00:00
internal fix: Fix error messages when a comprehension iterates over a non-array. 2025-03-21 16:37:59 +00:00
linter fix: Fix error messages when a comprehension iterates over a non-array. 2025-03-21 16:37:59 +00:00
python port some Python wrapper code fixes from C++ jsonnet 2025-01-20 21:45:24 +00:00
testdata chore: add test cases for std.manifestYamlDoc error reporting 2025-06-19 15:46:07 +01:00
toolutils feat(parser): export parser.Children (#327) 2019-10-06 22:38:37 +02:00
.bazelignore feat: add an example bazel module + go binary using the library 2025-03-16 16:10:31 +00:00
.bazelversion ci: pin bazel version to 7.5.0, and require up-to-date lockfile for CI build 2025-03-12 20:18:09 +00:00
.gitignore feat: add an example bazel module + go binary using the library 2025-03-16 16:10:31 +00:00
.gitmodules Add C++-Jsonnet submodule for tests (#202) 2018-03-06 13:05:00 -05:00
.golangci.yml Remove dependency on gen to generate sets (#746) 2024-06-09 19:24:41 +01:00
.goreleaser.yml ci: fix incorrect use of target_commitish in goreleaser release 2025-02-22 23:18:43 +00:00
.pre-commit-hooks.yaml updated pre-commit config and readme 2021-05-19 16:01:22 +02:00
benchmark.sh feat: Improve performance of substr by 84.44% 2020-01-26 09:20:58 +01:00
BUILD.bazel update Bazel rules with gazelle and manually 2025-01-20 21:45:24 +00:00
builtins_benchmark_test.go feat: Go implementation for manifestYamlDoc and escapeStringJson (#742) 2024-06-09 19:27:15 +01:00
builtins.go fix: Unchecked error in std.manifestYamlDoc(). 2025-06-19 13:46:03 +01:00
CONTRIBUTING Add CONTRIBUTING, add newline to end of other files 2016-02-19 17:36:19 -05:00
debugger.go fix: use debugValueToString for debugger operations (#759) 2024-06-11 14:40:04 +01:00
doc.go Remove go:generate causing errors 2017-09-29 12:36:04 -04:00
error_formatter.go feat: add debugger support (#739) 2024-06-09 15:09:24 +01:00
go.mod chore: Switch to Go 1.23.7 and update dependencies (#794) 2025-03-11 03:29:01 +00:00
go.sum chore: Switch to Go 1.23.7 and update dependencies (#794) 2025-03-11 03:29:01 +00:00
imports.go chore: remove refs to deprecated io/ioutil (#716) 2024-06-10 21:27:07 +01:00
interpreter_test.go First end-to-end test (addition of numbers) 2016-03-08 01:17:44 -05:00
interpreter.go Fix field visibility in objectHas for extended objects (#737) 2024-06-10 21:26:12 +01:00
jsonnet_test.go Add 'importbin' statement 2022-03-03 22:49:02 +00:00
LICENSE Add CONTRIBUTING, add newline to end of other files 2016-02-19 17:36:19 -05:00
main_test.go chore: remove refs to deprecated io/ioutil (#716) 2024-06-10 21:27:07 +01:00
Makefile Remove dependency on gen to generate sets (#746) 2024-06-09 19:24:41 +01:00
MANIFEST.in Prepare for v0.18.0 2021-12-21 18:34:50 +00:00
MODULE.bazel release: prepare to release v0.21.0 2025-05-07 16:12:02 +01:00
MODULE.bazel.lock ci: pin bazel version to 7.5.0, and require up-to-date lockfile for CI build 2025-03-12 20:18:09 +00:00
pyproject.toml ci: copy the publish-python.yml workflow from C++ jsonnet 2025-02-22 17:30:56 +00:00
README.md doc: add an explanatory sentence in the REAMDE about WASM 2025-05-08 13:17:05 +01:00
runtime_error.go feat: add debugger support (#739) 2024-06-09 15:09:24 +01:00
setup.py Fix compatibility with setuptools 72 2025-03-12 23:29:18 +00:00
tests.sh default to python3 2021-12-21 13:06:57 +00:00
thunks.go Fix field visibility in objectHas for extended objects (#737) 2024-06-10 21:26:12 +01:00
update_cpp_jsonnet.sh chore: teach update_cpp_jsonnet.sh to pull a named release version 2025-02-22 22:07:32 +00:00
util.go Add github actions ci 2021-05-19 16:00:28 +02:00
value.go Fix field visibility in objectHas for extended objects (#737) 2024-06-10 21:26:12 +01:00
vm.go release: prepare to release v0.21.0 2025-05-07 16:12:02 +01:00
WORKSPACE Add github actions ci 2021-05-19 16:00:28 +02:00
yaml.go Resolves a false-positive detection of multi-doc YAML streams (#693) 2023-05-03 19:37:04 +01:00

go-jsonnet

GoDoc Widget Travis Widget Coverage Status Widget

This an implementation of Jsonnet in pure Go. It is a feature complete, production-ready implementation. It is compatible with the original Jsonnet C++ implementation. Bindings to C and Python are available (but not battle-tested yet).

This code is known to work on Go 1.23 and above. We recommend always using the newest stable release of Go.

Installation instructions

# Using `go get` to install binaries is deprecated.
# The version suffix is mandatory.
go install github.com/google/go-jsonnet/cmd/jsonnet@latest

# Or other tools in the 'cmd' directory
go install github.com/google/go-jsonnet/cmd/jsonnet-lint@latest

It's also available on Homebrew:

brew install go-jsonnet

jsonnetfmt and jsonnet-lint are also available as pre-commit hooks. Example .pre-commit-config.yaml:

- repo: https://github.com/google/go-jsonnet
  rev: # ref you want to point at, e.g. v0.17.0
  hooks:
    - id: jsonnet-format
    - id: jsonnet-lint

It can also be embedded in your own Go programs as a library:

package main

import (
	"fmt"
	"log"

	"github.com/google/go-jsonnet"
)

func main() {
	vm := jsonnet.MakeVM()

	snippet := `{
		person1: {
		    name: "Alice",
		    welcome: "Hello " + self.name + "!",
		},
		person2: self.person1 { name: "Bob" },
	}`

	jsonStr, err := vm.EvaluateAnonymousSnippet("example1.jsonnet", snippet)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(jsonStr)
	/*
	   {
	     "person1": {
	         "name": "Alice",
	         "welcome": "Hello Alice!"
	     },
	     "person2": {
	         "name": "Bob",
	         "welcome": "Hello Bob!"
	     }
	   }
	*/
}

Build instructions (go 1.23+)

git clone git@github.com:google/go-jsonnet.git
cd go-jsonnet
go build ./cmd/jsonnet
go build ./cmd/jsonnetfmt
go build ./cmd/jsonnet-deps

To build with Bazel instead:

git clone git@github.com:google/go-jsonnet.git
cd go-jsonnet
git submodule init
git submodule update
bazel build //cmd/jsonnet
bazel build //cmd/jsonnetfmt
bazel build //cmd/jsonnet-deps

The resulting jsonnet program will then be available at a platform-specific path, such as bazel-bin/cmd/jsonnet/darwin_amd64_stripped/jsonnet for macOS.

Bazel also accommodates cross-compiling the program. To build the jsonnet program for various popular platforms, run the following commands:

Target platform Build command
Current host bazel build //cmd/jsonnet
Linux bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 //cmd/jsonnet
macOS bazel build --platforms=@io_bazel_rules_go//go/toolchain:darwin_amd64 //cmd/jsonnet
Windows bazel build --platforms=@io_bazel_rules_go//go/toolchain:windows_amd64 //cmd/jsonnet

For additional target platform names, see the per-Go release definitions here in the rules_go Bazel package.

Additionally if any files were moved around, see the section Keeping the Bazel files up to date.

Building libjsonnet.wasm

The WASM build can be used to embed go-jsonnet for use (client side) in the web browser. This is used for the live code snippets on https://jsonnet.org/

GOOS=js GOARCH=wasm go build -o libjsonnet.wasm ./cmd/wasm 

Or if using bazel:

bazel build //cmd/wasm:libjsonnet.wasm

Running tests

./tests.sh  # Also runs `go test ./...`

Running Benchmarks

Method 1

go get golang.org/x/tools/cmd/benchcmp
  1. Make sure you build a jsonnet binary prior to making changes.
go build -o jsonnet-old ./cmd/jsonnet
  1. Make changes (iterate as needed), and rebuild new binary
go build ./cmd/jsonnet
  1. Run benchmark:
# e.g. ./benchmark.sh Builtin
./benchmark.sh <TestNameFilter>

Method 2

  1. get benchcmp
go get golang.org/x/tools/cmd/benchcmp
  1. Make sure you build a jsonnet binary prior to making changes.
make build-old
  1. iterate with (which will also automatically rebuild the new binary ./jsonnet)

replace the FILTER with the name of the test you are working on

FILTER=Builtin_manifestJsonEx make benchmark

Update cpp-jsonnet sub-repo

This repo depends on the original Jsonnet repo. Shared parts include the standard library, headers files for C API and some tests.

You can update the submodule and regenerate dependent files with one command:

./update_cpp_jsonnet.sh

Note: It needs to be run from repo root.

Updating and modifying the standard library

Standard library source code is kept in cpp-jsonnet submodule, because it is shared with Jsonnet C++ implementation.

For performance reasons we perform preprocessing on the standard library, so for the changes to be visible, regeneration is necessary:

go run cmd/dumpstdlibast/dumpstdlibast.go cpp-jsonnet/stdlib/std.jsonnet > astgen/stdast.go

**The

The above command creates the astgen/stdast.go file which puts the desugared standard library into the right data structures, which lets us avoid the parsing overhead during execution. Note that this step is not necessary to perform manually when building with Bazel; the Bazel target regenerates the astgen/stdast.go (writing it into Bazel's build sandbox directory tree) file when necessary.

Keeping the Bazel files up to date

Note that we maintain the Go-related Bazel targets with the Gazelle tool. The Go module (go.mod in the root directory) remains the primary source of truth. Gazelle analyzes both that file and the rest of the Go files in the repository to create and adjust appropriate Bazel targets for building Go packages and executable programs.

After changing any dependencies within the files covered by this Go module, it is helpful to run go mod tidy to ensure that the module declarations match the state of the Go source code. In order to synchronize the Bazel rules with material changes to the Go module, run the following command to invoke Gazelle's update-repos command:

bazel run //:gazelle -- update-repos -from_file=go.mod -to_macro=bazel/deps.bzl%jsonnet_go_dependencies

Similarly, after adding or removing Go source files, it may be necessary to synchronize the Bazel rules by running the following command:

bazel run //:gazelle