CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-jsonnet

Jsonnet is a data templating language that extends JSON with variables, conditionals, functions, and imports for configuration management

Pending
Overview
Eval results
Files

cli-tools.mddocs/

Command Line Tools

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.

Capabilities

jsonnet Command

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 JSON

jsonnetfmt Command

Code 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 debugging

Usage Examples

Basic jsonnet Usage

Evaluate file to stdout:

jsonnet config.jsonnet

Save output to file:

jsonnet -o output.json config.jsonnet

Evaluate code directly:

jsonnet -e '{ greeting: "Hello World!" }'

External Variables

String variables:

jsonnet -V environment=production -V region=us-west-1 config.jsonnet

Code variables:

jsonnet -C database_config='{ host: "db.prod.com", port: 5432 }' config.jsonnet

Mixed variables:

jsonnet \
  -V env=production \
  -V debug=false \
  -C features='["auth", "logging"]' \
  -A service_name=api-server \
  config.jsonnet

Top-Level Arguments

For Jsonnet files that are functions:

# deployment.jsonnet is a function: function(cluster, replicas=3) { ... }
jsonnet -A cluster=production --tla-code replicas=5 deployment.jsonnet

Import Paths

jsonnet -J /usr/local/lib/jsonnet -J ./lib config.jsonnet

Multi-File Output

# 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

JSON Stream Output

# For Jsonnet that outputs an array of objects
jsonnet -S stream-config.jsonnet

# Outputs each array element as a separate JSON object

YAML Output

jsonnet --yaml-out config.jsonnet > config.yaml
jsonnet --yaml-stream stream-config.jsonnet > stream.yaml

VM Configuration

jsonnet \
  --max-stack 1000 \
  --gc-min-objects 5000 \
  --gc-growth-trigger 3.0 \
  --max-trace 50 \
  large-config.jsonnet

String Output Mode

# Output raw strings without JSON encoding
jsonnet --string-output -e '"Hello World!"'
# Output: Hello World! (not "Hello World!")

jsonnetfmt Usage

Basic Formatting

Format to stdout:

jsonnetfmt config.jsonnet

Format in place:

jsonnetfmt -i config.jsonnet

Format multiple files:

jsonnetfmt -i *.jsonnet lib/*.jsonnet

Test formatting (exit code only):

jsonnetfmt --test config.jsonnet
echo $?  # 0 if correctly formatted, 1 if needs formatting

Style Configuration

Custom indentation:

jsonnetfmt --indent 4 config.jsonnet

String and comment style:

jsonnetfmt --string-style s --comment-style h config.jsonnet

Padding and spacing:

jsonnetfmt --pad-arrays --pad-objects config.jsonnet

Import sorting:

jsonnetfmt --sort-imports --pretty-field-names config.jsonnet

Complete 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 *.jsonnet

Integration Examples

Build System Integration

Makefile:

%.json: %.jsonnet
	jsonnet -o $@ $<

config-prod.json: config.jsonnet
	jsonnet -V environment=production -o $@ $<

format:
	jsonnetfmt -i *.jsonnet

.PHONY: format

Shell 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"

CI/CD Pipeline

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/null

Docker Integration

Dockerfile:

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.jsonnet

Advanced Usage

Configuration 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
done

With 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.jsonnet

Validation 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

docs

c-library.md

cli-tools.md

cpp-wrapper.md

formatting.md

index.md

python-bindings.md

standard-library.md

tile.json