From 3ab58cd603e0e5d9faa900a0b8f37e71c7ed211e Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Mon, 19 May 2025 13:57:56 +0100 Subject: [PATCH] docs: Warn about use of heredoc for JSON/YAML (#37084) * docs: Warn about use of heredoc for JSON/YAML * move block near the end of the section --- website/docs/language/expressions/strings.mdx | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/website/docs/language/expressions/strings.mdx b/website/docs/language/expressions/strings.mdx index 99eb6775d2..e590c9380d 100644 --- a/website/docs/language/expressions/strings.mdx +++ b/website/docs/language/expressions/strings.mdx @@ -47,7 +47,7 @@ There are also two special escape sequences that do not use backslashes: ## Heredoc Strings -Terraform also supports a "heredoc" style of string literal inspired by Unix +Terraform supports a "heredoc" style of string literal inspired by Unix shell languages, which allows multi-line strings to be expressed more clearly. ```hcl @@ -74,20 +74,25 @@ 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](/terraform/language/functions/jsonencode) or [the `yamlencode` function](/terraform/language/functions/yamlencode) so that Terraform can be responsible for guaranteeing valid JSON or YAML syntax. + + ```hcl - example = jsonencode({ - a = 1 - b = "hello" - }) +example = jsonencode({ + a = 1 + b = "hello" +}) ``` + + + ### Indented Heredocs The standard heredoc form (shown above) treats all space characters as literal