Jsonnet Python Bindings

0.22.0 · active · verified Sat Apr 11

Jsonnet is a data templating language that helps you generate configuration data. The `jsonnet` Python library provides official Python bindings for the original C++ implementation, allowing evaluation of Jsonnet code from Python. The current version is 0.22.0, released on March 24, 2026. Releases are made periodically, typically driven by updates to the core Jsonnet language and its C++ implementation.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to evaluate a Jsonnet snippet using `_jsonnet.evaluate_snippet` and access external variables. The output is a JSON string, which is then parsed into a Python dictionary.

import _jsonnet
import json

# Example Jsonnet snippet
jsonnet_snippet = '''
local person(name='Alice') = {
  name: name,
  welcome: 'Hello ' + name + '!'
};
{
  person1: person(),
  person2: person('Bob'),
  env_greeting: 'Hello from env ' + std.extVar('GREET_TARGET'),
}
'''

# Evaluate a Jsonnet snippet
# Note: For security, never pass untrusted input to ext_vars without proper sanitization/validation.
output_json_string = _jsonnet.evaluate_snippet(
    'example.jsonnet', # Filename used in error messages
    jsonnet_snippet,
    ext_vars={'GREET_TARGET': 'World'}
)

# Parse the resulting JSON string into a Python object
output_data = json.loads(output_json_string)

print(json.dumps(output_data, indent=2))

view raw JSON →