From 776b33db329a15c82a910bcf9c4e76fba25e0d42 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Mon, 7 Dec 2020 15:25:57 -0800 Subject: [PATCH] website: Recommend against making JSON/YAML with heredocs This is a repeated cause of confusion and questions in the community forum, because both JSON and YAML valid syntax are hard to generate using just string concatenation. Terraform has built-in functions for both of these common serializations to avoid those problems, and so this will hopefully make these better alternatives more discoverable. --- .../docs/configuration/expressions/strings.html.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/website/docs/configuration/expressions/strings.html.md b/website/docs/configuration/expressions/strings.html.md index 4841a39e7..d4f15d742 100644 --- a/website/docs/configuration/expressions/strings.html.md +++ b/website/docs/configuration/expressions/strings.html.md @@ -72,6 +72,20 @@ In the above example, `EOT` is the identifier selected. Any identifier is allowed, but conventionally this identifier is in all-uppercase and begins with `EO`, meaning "end of". `EOT` in this case stands for "end of text". +### Generating JSON or YAML + +Don't use "heredoc" strings to generate JSON or YAML. Instead, use +[the `jsonencode` function](../functions/jsonencode.html) or +[the `yamlencode` function](../functions/yamlencode.html) so that Terraform +can be responsible for guaranteeing valid JSON or YAML syntax. + +```hcl + example = jsonencode({ + a = 1 + b = "hello" + }) +``` + ### Indented Heredocs The standard heredoc form (shown above) treats all space characters as literal