Search code examples
jsonnet

How to printf in jsonnet?


Is there a way to print the objects in jsonnet? This is for debugging purposes mainly.

I am using error to print the objects but that terminates the program execution.

local obj = [
{
  myKey: 2,
}];
error 'Printing' + std.toString(obj)

Outputs:

RUNTIME ERROR: Printing[{"myKey": 2}]
    snippet:6:1-37  

A better way to do this ?


Solution

  • To followup on Dave Cunningham's answer, std.trace() is available since 0.11.0, it behaves like a "hook in the middle", where it's 1st argument is the string you want to show, 2nd is what you want to return.

    Using it for the provided example:

    $ cat foo.jsonnet
    local obj = [
    {
        myKey: 2,
    }];
    std.trace("obj content: %s" % [obj], obj)
    
    $ jsonnet foo.jsonnet 
    TRACE: foo.jsonnet:5 obj content: [{"myKey": 2}]
    [
       {
          "myKey": 2
       }
    ]