7

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 ?

asked Apr 9, 2018 at 22:21

3 Answers 3

8

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
 }
]
answered May 11, 2018 at 19:57
Sign up to request clarification or add additional context in comments.

Comments

2

For posterity, OP was kind enough to contribute a new function to solve this problem: std.trace("message", rest).

answered Apr 16, 2018 at 4:33

2 Comments

you should elaborate your answer
@catchingUp fyi provided an example, based on the original question
1

[obsolete, see other answers]

At the moment (as of jsonnet 0.10) unfortunately no. There are plans to include it in a future release (issue here: https://github.com/google/jsonnet/issues/130).

People use errors (like you have shown in the code) or modify the code to output just the part they want (how handy it is depends on how you structured your code).

answered Apr 10, 2018 at 17:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.