jsonnet: Generate JSON Config From Templates
jsonnet evaluates a .jsonnet program into JSON, giving config functions, variables, and imports without a general-purpose language.
jsonnet is the config language behind Grafana Tanka and many Kubernetes setups. It is deterministic and pure, so the same input always renders the same output.
What it does
jsonnet evaluates a file (or expression) and prints the resulting JSON. External variables come in with --ext-str/--ext-code, top-level arguments with --tla-str/--tla-code. -y emits a YAML stream and -m writes a JSON object of filenames to multiple files.
Common usage
jsonnet config.jsonnet > config.json
# pass an external variable
jsonnet --ext-str env=prod config.jsonnet > config.json
# multi-file output: one file per top-level key
jsonnet -m ./out config.jsonnetOptions
| Flag | What it does |
|---|---|
| -e, --exec | Evaluate the argument as an inline expression |
| --ext-str <var=val> | Set an external string variable (std.extVar) |
| --ext-code <var=code> | Set an external variable to evaluated code |
| --tla-str <var=val> | Top-level argument as a string |
| -m, --multi <dir> | Write each top-level field to its own file |
| -y, --yaml-stream | Emit a YAML stream of an array of documents |
| -J, --jpath <dir> | Add a library search path for import |
In CI
Pin the jsonnet version (google/jsonnet vs the faster go-jsonnet can differ subtly) and add jsonnet-bundler paths with -J so imports resolve. Render to files and diff; because jsonnet is pure, a diff is a real config change, never nondeterminism.
Common errors in CI
"RUNTIME ERROR: Undefined external variable: env" means you referenced std.extVar("env") without passing --ext-str env=.... "STATIC ERROR: <file>:N:M: Unknown variable: x" is a typo caught before evaluation. "RUNTIME ERROR: field does not exist: foo" is accessing a missing object key. "RUNTIME ERROR: couldn't open import \"lib.libsonnet\"" means the -J search path is missing.