Output Formats

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.

Custom Output Formats

The INI format provides a simple example.

  • The content of the INI file has been represented within the JSON object model, i.e. using objects to represent the nesting of the sections.
  • The special main section holds the top-level keys (ones not in any section).
  • The function std.manifestIni (written in Jsonnet) serializes the structure to a string.
  • Jsonnet is run in string output mode -S to print the string verbatim rather than encoded as JSON.

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.

output.ini

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.

output.svg

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.