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.

A Warning On Server-Side Evaluation

If you are creating an API server that will evaluate untrusted Jsonnet code in the same process that handles other requests, be aware of your exposure to denial of service attacks. Since Jsonnet allows expressing programs that take unreasonable amounts of CPU time and RAM, it is possible for a malicious request to cause its host API server to exceed its OS resources and crash. Such a crash would affect concurrently running handlers for other users, and decrease your capacity until the server is restarted. A simple remedy for this situation is to exec the Jsonnet commandline binary in a ulimit child process where the CPU and RAM can be tightly bounded for the specific request. Communication to and from the commandline binary could be via temporary files or pipes. Thus, only the malicious request fails.

Linking Jsonnet as a library is more appropriate for client-side applications or situations where the configuration can be trusted.

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.