Given the following jsonnetfile:
{
expression: self.result,
result: 3 > 1,
}
That evaluates to this:
{
"expression": true,
"result": true
}
Is there a way to convert the expression to string before evaluation? The desired result would be the following:
{
"expression": "3 > 1",
"result": true
}
As jsonnet
doesn't support eval()
- alike expressions, the only way really is to load such file as text with importstr()
(then conveniently parse it as YAML¹), also with import()
to get it parsed as jsonnet
, then "merge" both objects to cherry-pick fields from each one.
Below is a possible approach for the above, of course, it could be generalized for more "complex" root object structure, but it's enough to show the approach.
Note that your original jsonnetfile
is saved as foo.yaml
, but with expression
and result
fields swapped (as I think this is your intent)
{
expression: 3 > 1,
result: self.expression,
}
local asYAML = std.parseYaml(importstr 'foo.yaml');
local asJsonnet = import 'foo.yaml';
{
expression: asYAML.expression,
result: asJsonnet.result,
}
{
"expression": "3 > 1",
"result": true
}
¹ Note that loading jsonnet as YAML will only support a subset of jsonnet, as YAML parser may interfere with other language expressions