Currently (0.13.5), the format(“format”,“inputs”) function flattens map outputs:
echo ‘format(“myout: %v”,{ b = “b”, a = “a” })’ | terraform console
myout: {“a”:“a”,“b”:“b”}
How can I get the output to be:
myout: { "a":"a", "b":"b" }
Currently (0.13.5), the format(“format”,“inputs”) function flattens map outputs:
echo ‘format(“myout: %v”,{ b = “b”, a = “a” })’ | terraform console
myout: {“a”:“a”,“b”:“b”}
How can I get the output to be:
myout: { "a":"a", "b":"b" }
Try tomap function tomap - Functions - Configuration Language - Terraform by HashiCorp
> tomap({"a" = 1, "b" = 2})
{
"a" = 1
"b" = 2
}
Hi @donald.bower,
I think in this particular case probably the best answer with today’s Terraform language would be to use the jsonencode
function instead, which produces non-compact JSON:
jsonencode({ b = “b”, a = “a” }}
If you need that myout:
prefix then you can use normal template interpolation to achieve that:
"myout: ${jsonencode({ b = “b”, a = “a” }}}"
If you’re using format
because in your real example you’re using some other formatting features other than just concatenation of a prefix, you can still do that by using the %s
verb to include the jsonencode
result:
format("myout: %s", jsonencode({ b = “b”, a = “a” }))
I hope one of those options gives you a starting point for getting the result you need.
Returning to this a little later, I realize that I didn’t actually address what you asked about because I was distracted when I wrote this and was thinking about yamlencode
rather than jsonencode
.
jsonencode
also generates the most compact form of JSON, so what I wrote here wouldn’t help you.
With that said, if you just output the JSON without any special prefix then Terraform’s value renderer has a special heuristic for that where it’ll show you the effective value of the JSON rather than the literal string, so if your aim is only to get a readable value for display then removing that myout:
prefix might get you there with Terraform as it currently exists.