Jsonnet Python Bindings
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
- breaking Between versions 0.21.0 and 0.22.0, bitwise operation arguments are now limited to the 'safe-integer' range (IEEE754 double-precision floating-point integers). Code relying on bitwise operations outside this range will now error. The internal `std.objectRemoveKey` function was also re-implemented to fix several bugs and unexpected behaviors, which might subtly change outcomes for complex object manipulations.
- breaking Pre-built Python binary wheels (used during `pip install`) now use the Python Limited API (ABI3 compatible) since version 0.22.0. This improves compatibility with newer Python versions, but might require recompilation or specific build environments if you are building from source against an older or non-standard Python setup.
- gotcha The `_jsonnet` library, being a binding to the C++ implementation, is not hardened for untrusted inputs and poses significant security risks (data exfiltration, denial of service) if used to evaluate arbitrary, untrusted Jsonnet code. The `import`, `importstr`, and `importbin` constructs can read any file accessible to the interpreter process.
- gotcha When using `_jsonnet.evaluate_snippet()` for Jsonnet code that contains `import` statements, the interpreter needs to know where to search for imported files. Without specifying `jpathdir` or providing a custom `import_callback`, these imports will fail.
- gotcha Jsonnet is a lazy evaluation language. This means expressions are only evaluated when their value is actually needed. This can lead to unexpected behavior regarding errors or performance, as code that appears before an error-inducing line might not be evaluated, or computationally intensive parts might only run when accessed.
Install
-
pip install jsonnet
Imports
- _jsonnet
import _jsonnet
Quickstart
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))