Reputation: 1460
I have this expression in the terraform console:
jsonencode([for x in ["a", "b"] : { x = "hello" }])
It gives this output:
[{"x":"hello"},{"x":"hello"}]
Why it is interpreting "x" literally and not as a variable? This is my desired output:
[{"a":"hello"},{"b":"hello"}]
Upvotes: 1
Views: 204
Reputation: 238887
This happens because unquoted strings in this context are considered as map's keys:
The keys in a map must be strings; they can be left unquoted if they are a valid identifier, but must be quoted otherwise.
If you want to treat x
as variable you either use it in quotes or parentheses:
jsonencode([for x in ["a", "b"] : { (x) = "hello" }])
or
jsonencode([for x in ["a", "b"] : { "${x}" = "hello" }])
Upvotes: 3