Jsonnet is a data templating language that extends JSON with variables, conditionals, functions, and imports for configuration management
—
Jsonnet provides two command-line tools: jsonnet for evaluating Jsonnet code and generating JSON output, and jsonnetfmt for formatting and linting Jsonnet source code. Both tools offer extensive configuration options and support various output modes.
Primary command-line tool for evaluating Jsonnet files and generating JSON output with support for multiple output modes, external variables, and import path configuration.
jsonnet [options] <filename>
# Core Options:
-o, --output-file <file> Write output to file instead of stdout
-m, --multi <dir> Write multiple files to directory
-S, --stream Write JSON stream output
-e, --exec Evaluate argument as Jsonnet code instead of filename
# Variable Options:
-V, --ext-str <var>=<val> External string variable
-C, --ext-code <var>=<val> External code variable
-A, --tla-str <var>=<val> Top-level string argument
--tla-code <var>=<val> Top-level code argument
# Import Options:
-J, --jpath <dir> Add library search directory
# VM Options:
--max-stack <n> Maximum stack depth (default: 500)
--gc-min-objects <n> GC minimum objects (default: 1000)
--gc-growth-trigger <x> GC growth trigger (default: 2.0)
--max-trace <n> Maximum stack trace lines (default: 20)
--string-output Output raw string instead of JSON
# Format Options:
--yaml-stream Output YAML stream format
--yaml-out Output YAML format instead of JSONCode formatter for Jsonnet source code with extensive style configuration options and support for in-place editing.
jsonnetfmt [options] [filename...]
# Core Options:
-i, --in-place Modify files in place
-o, --output-file <file> Write output to file
--test Test if input is correctly formatted (exit status only)
# Style Options:
--indent <n> Indentation level (default: 2 spaces)
--max-blank-lines <n> Maximum consecutive blank lines (default: 2)
--string-style <style> String literal style: d(ouble), s(ingle), l(eave)
--comment-style <style> Comment style: h(ash), s(lash), l(eave)
# Formatting Options:
--pad-arrays Add extra space inside arrays
--pad-objects Add extra space inside objects
--pretty-field-names Use syntax sugar for field names when possible
--sort-imports Sort top-level imports alphabetically
--debug-desugaring Show desugared output for debuggingEvaluate file to stdout:
jsonnet config.jsonnetSave output to file:
jsonnet -o output.json config.jsonnetEvaluate code directly:
jsonnet -e '{ greeting: "Hello World!" }'String variables:
jsonnet -V environment=production -V region=us-west-1 config.jsonnetCode variables:
jsonnet -C database_config='{ host: "db.prod.com", port: 5432 }' config.jsonnetMixed variables:
jsonnet \
-V env=production \
-V debug=false \
-C features='["auth", "logging"]' \
-A service_name=api-server \
config.jsonnetFor Jsonnet files that are functions:
# deployment.jsonnet is a function: function(cluster, replicas=3) { ... }
jsonnet -A cluster=production --tla-code replicas=5 deployment.jsonnetjsonnet -J /usr/local/lib/jsonnet -J ./lib config.jsonnet# For Jsonnet that outputs an object with filenames as keys
jsonnet -m ./output-dir multi-file-config.jsonnet
# Creates multiple files in output-dir/
# Each key becomes a filename, each value becomes file content# For Jsonnet that outputs an array of objects
jsonnet -S stream-config.jsonnet
# Outputs each array element as a separate JSON objectjsonnet --yaml-out config.jsonnet > config.yaml
jsonnet --yaml-stream stream-config.jsonnet > stream.yamljsonnet \
--max-stack 1000 \
--gc-min-objects 5000 \
--gc-growth-trigger 3.0 \
--max-trace 50 \
large-config.jsonnet# Output raw strings without JSON encoding
jsonnet --string-output -e '"Hello World!"'
# Output: Hello World! (not "Hello World!")Format to stdout:
jsonnetfmt config.jsonnetFormat in place:
jsonnetfmt -i config.jsonnetFormat multiple files:
jsonnetfmt -i *.jsonnet lib/*.jsonnetTest formatting (exit code only):
jsonnetfmt --test config.jsonnet
echo $? # 0 if correctly formatted, 1 if needs formattingCustom indentation:
jsonnetfmt --indent 4 config.jsonnetString and comment style:
jsonnetfmt --string-style s --comment-style h config.jsonnetPadding and spacing:
jsonnetfmt --pad-arrays --pad-objects config.jsonnetImport sorting:
jsonnetfmt --sort-imports --pretty-field-names config.jsonnetComplete style configuration:
jsonnetfmt \
--indent 4 \
--max-blank-lines 1 \
--string-style d \
--comment-style s \
--pad-arrays \
--pad-objects \
--pretty-field-names \
--sort-imports \
-i *.jsonnetMakefile:
%.json: %.jsonnet
jsonnet -o $@ $<
config-prod.json: config.jsonnet
jsonnet -V environment=production -o $@ $<
format:
jsonnetfmt -i *.jsonnet
.PHONY: formatShell script for deployment:
#!/bin/bash
set -e
ENVIRONMENT=${1:-development}
OUTPUT_DIR="./generated"
echo "Generating configuration for environment: $ENVIRONMENT"
mkdir -p "$OUTPUT_DIR"
jsonnet \
-V environment="$ENVIRONMENT" \
-V timestamp="$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
-C build_info="$(git rev-parse HEAD)" \
-m "$OUTPUT_DIR" \
deployment.jsonnet
echo "Configuration generated in $OUTPUT_DIR"GitHub Actions:
name: Validate Jsonnet
on: [push, pull_request]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Jsonnet
run: |
wget https://github.com/google/jsonnet/releases/download/v0.21.0/jsonnet-bin-v0.21.0-linux.tar.gz
tar -xzf jsonnet-bin-v0.21.0-linux.tar.gz
sudo mv jsonnet jsonnetfmt /usr/local/bin/
- name: Check formatting
run: |
find . -name "*.jsonnet" -exec jsonnetfmt --test {} \;
- name: Validate syntax
run: |
find . -name "*.jsonnet" -exec jsonnet {} \; > /dev/nullDockerfile:
FROM alpine:latest
RUN apk add --no-cache jsonnet
COPY configs/ /configs/
WORKDIR /configs
ENTRYPOINT ["jsonnet"]
CMD ["--help"]Usage:
docker build -t my-jsonnet .
docker run --rm -v $(pwd)/output:/output my-jsonnet -m /output config.jsonnetConfiguration management:
#!/bin/bash
# generate-configs.sh
environments=("development" "staging" "production")
services=("api" "worker" "scheduler")
for env in "${environments[@]}"; do
for service in "${services[@]}"; do
output_file="${service}-${env}.json"
echo "Generating $output_file..."
jsonnet \
-V environment="$env" \
-V service="$service" \
-A replicas="$(if [ "$env" = "production" ]; then echo 3; else echo 1; fi)" \
-o "configs/$output_file" \
templates/service.jsonnet
done
doneWith templating and includes:
# Create library of reusable components
mkdir -p lib/
# Generate configurations with common libraries
jsonnet \
-J lib/ \
-J /usr/local/share/jsonnet-libs/ \
-V cluster="$(kubectl config current-context)" \
-C secrets="$(kubectl get secret app-secrets -o json)" \
-m ./k8s-manifests/ \
kubernetes.jsonnetValidation and testing:
#!/bin/bash
# validate-configs.sh
echo "Validating Jsonnet syntax..."
find . -name "*.jsonnet" | while read -r file; do
if ! jsonnet "$file" > /dev/null 2>&1; then
echo "Syntax error in $file"
exit 1
fi
done
echo "Checking formatting..."
if ! find . -name "*.jsonnet" -exec jsonnetfmt --test {} \;; then
echo "Formatting issues found. Run: find . -name '*.jsonnet' -exec jsonnetfmt -i {} \;"
exit 1
fi
echo "All validations passed!"Install with Tessl CLI
npx tessl i tessl/pypi-jsonnet