The data templating language
Jsonnet

Libraries For Other Languages

If you don't want to invoke the jsonnet commandline utility to evaluate Jsonnet, there are also libraries for Python and C. In future we would like to support more languages by wrapping the C API in each case.

C API

The C API simply wraps the C++ implementation to hide C++ language features like templates, classes, overloading, etc. This makes it easier to bind to other languages, as C is typically the lowest common denominator across all systems.

The API is documented in libjsonnet.h. It is built with 'make libjsonnet.so'. It is used by the python bindings, the Jsonnet commandline tool, as well as a couple of simpler tests. Search for #include "libjsonnet.h".

To use the API, create a JsonnetVM object, set various options, then tell it to evaluate a filename or snippet. To avoid leaking memory, the result of execution (JSON or error message) and the JsonnetVM object itself must be cleaned up using the corresponding functions.

Python API

The Python API wraps the C API in a straightforward way. It can be installed with pip install jsonnet or built directly with setup.py.

The Python module provides two functions, evaluate_file(f) and evaluate_snippet(f, e). In the latter case, the parameter f is used in stack traces due to errors raised in the snippet.

Keyword arguments to these functions are used to control the virtual machine. They include gc_min_objects, gc_growth_trigger, ext_vars, debug_ast, and max_trace. The debug_ast argument is a boolean, and ext_vars is a dict mapping strings to strings. The other keyword arguments should be numbers.

Another keyword argument import_callback can be used to pass a callable to trap the Jsonnet import constructs. This allows, e.g., reading files out of archives or implementing library search paths. The supplied function must take two string arguments (directory of the current file and the string given to the import construct, which can usually be concatenated to form an actual path but that is up to you). It returns a tuple of two strings, the first being the actual path of the file and the second being the content. The actual path is required so that imports can be resolved within the imported file.

If an error is raised during the evaluation of the Jsonnet code, it is formed into a stack trace and thrown as a python RuntimeError. Otherwise, the JSON string is returned. To convert this into objects for easy interpretation in Python, use the json module. An example:

import json
import _jsonnet

jsonnet_str = '''
{
    person1: {
        name: "Alice",
        welcome: "Hello " + self.name + "!",
    },
    person2: self.person1 { name: std.extVar("OTHER_NAME") },
}
'''

json_str = _jsonnet.evaluate_snippet("snippet", jsonnet_str, ext_vars={'OTHER_NAME': 'Bob'})
json_obj = json.loads(json_str)
for person_id, person in json_obj.iteritems():
    print('%s is %s, greeted by "%s"' % (person_id, person['name'], person['welcome']))

Unofficial Third Party APIs

There are unofficial bindings available for other languages. These are not supported by Google and may be some versions behind the latest release.