A Jsonnet evaluation typically outputs a JSON document. For ways to output multiple JSON documents, see Getting Started. Whether this integrates cleanly depends on your application. If you're writing a custom application, then you can simply consume this JSON. Another option is to write your config as a protocol buffer and deserialize Jsonnet's JSON output using the proto3 canonical JSON representation. Interoperation with YAML is also easy, see Kubernetes for an example. To support consumers of syntactically incompatible formats such as INI or XML, custom serialization is needed.
The INI format provides a simple example.
main
section holds the top-level keys (ones not in any section).
std.manifestIni
(written in Jsonnet) serializes the structure to
a string.
The code below could be executed with the command:
$ jsonnet -S ini.jsonnet
Try replacing manifestIni
. with manifestJson
or
manifestPython
. There are many such functions in the standard library, and you
can write custom ones too. Finally, to see the effect of -S directly, try
replacing the whole buffer with just a string literal.
Thus, despite internally using the JSON data model, Jsonnet can provide config generation for applications that consume INI.
Below is a more complex example where we construct an SVG file by emitting XML. It shows how to write a custom manifestation function, and also a non-trivial example of representing something in Jsonnet.
This approach generalizes widely because JSON is very versatile. Indeed, its success as an interchange format can be attributed to that versatility. Using the technique described above, Jsonnet can output any format that can be represented in JSON.