# Passing JSON literally inside a string literal

**URL:** <https://discuss.hashicorp.com/t/passing-json-literally-inside-a-string-literal/11859>\
**Category:** Terraform\
**Created:** [July 22, 2020, 11:55pm UTC](https://discuss.hashicorp.com/t/passing-json-literally-inside-a-string-literal/11859 "2020-07-22T23:55:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arcotek-ltd](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/arcotek-ltd/32/1674_2.png) [@arcotek-ltd](https://discuss.hashicorp.com/u/arcotek-ltd)\
**Post date:** [July 22, 2020, 11:55pm UTC](https://discuss.hashicorp.com/t/passing-json-literally-inside-a-string-literal/11859/1 "2020-07-22T23:55:24Z")

</div>

Hi,

I’m working with the [Azure virtual machine extension](https://www.terraform.io/docs/providers/azurerm/r/virtual_machine_extension.html) resource and struggling to pass the JSON object to `SETTINGS`.

Data comes from a `.auto.tfvar.json` file and in that file I have the following block:

```auto
//stuff above
"extensions" : [
              {
                "name" : "myCustomScript1-0-vm2",
                "publisher" : "Microsoft.Compute",
                "type" : "CustomScriptExtension",
                "type_handler_version" : "1.10",
                "auto_upgrade_minor_version" : "true",
                "settings" : {
                  "commandToExecute" : "powershell.exe -encodedCommand 'SoMeEnCoDeDsTrInG'"
                },
                "purpose" : "example extension 1-0"
              },...<repeat>...

```

I use nested for loops in a `local{}` block to iterate over this data in the resources `main.tf`:

```auto
locals {
  #vm_object = var.virtual_machine_object
  
  ext_config = flatten([
    for compute_key, compute in var.compute_data : [
      for node_key, node in compute.node_config : [
        for ext_key, ext in node.extensions : {
          id = "${compute_key}.${node_key}.${ext_key}"
          settings = ext.settings
          test = <<EOT ${ext.settings} EOT
          # other key/value pairs...
        }
      ]
    ]
  ])
}

```

Ignoring the `test` attribute for the minute, the block above works in that the output returns what I want:

```auto
{
  // Stuff above
  "publisher" = "Microsoft.Compute"
  "settings" = {
        "commandToExecute" = "powershell.exe -encodedCommand 'SoMeEnCoDeDsTrInG'"
  }
  "type" = "CustomScriptExtension"
}

```

According to the documentation, I need to get the contents of `settings`, as JSON, into the string literal `<<SETTINGS my_settings_json_block_here SETTINGS`.

I’ve tried a few things: `settings = each.value.settings`, the `test` attribute in the `for` block above, and the following being another (which is basically the same as `test`):

```auto
resource "azurerm_virtual_machine_extension" "pool" {
for_each = {
    for resource in local.ext_config :
    resource.id => resource
  }
type = each.value.type
# Other key/value pairs

settings = <<SETTINGS
    ${each.value.settings}
SETTINGS

```

With the above, during `plan` I am getting the error:

```auto
Error: Invalid template interpolation value

  on ..\azurerm_virtual_machine_extension\main.tf line 60, in resource "azurerm_virtual_machine_extension" "pool":
  59: 
  60: ${each.value.settings}
  61: 
    |----------------
    | each.value.settings is object with 1 attribute "commandToExecute"

Cannot include the given value in a string template: string required.

```

Wrapping the string template (`${each.value.settings}` is quotes doesn’t help, giving the same `string required` error.

I’ve looked at `jsonencode` and `jsondecode`, but I don’t think these will help as it is already JSON and I want JSON.

I’m running out of ideas, so if anyone can give me any pointers, I’d be most grateful.

T.I.A

---

<div class="post-metadata">

**Author:** ![arcotek-ltd](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/arcotek-ltd/32/1674_2.png) [@arcotek-ltd](https://discuss.hashicorp.com/u/arcotek-ltd)\
**Post date:** [July 23, 2020, 12:03am UTC](https://discuss.hashicorp.com/t/passing-json-literally-inside-a-string-literal/11859/2 "2020-07-23T00:03:59Z")

</div>

If I understand [this](https://www.terraform.io/docs/configuration/syntax-json.html#expression-mapping) document properly, I basically want to stop TF doing “Expression Mapping”:

> When a JSON string is encountered in a location where arbitrary expressions are expected, its value is first parsed as a [string template](https://www.terraform.io/docs/configuration/expressions.html#string-templates) and then it is evaluated to produce the final result.

---

<div class="post-metadata">

**Author:** ![alisdair](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/alisdair/32/4227_2.png) [@alisdair](https://discuss.hashicorp.com/u/alisdair)\
**Post date:** [July 23, 2020, 3:59pm UTC](https://discuss.hashicorp.com/t/passing-json-literally-inside-a-string-literal/11859/3 "2020-07-23T15:59:14Z")

</div>

From what I understand of your use case, it seems that `jsonencode` is likely to be what you’re looking for here. `each.value.settings` is an object type (as the diagnostic shows), but the attribute should be a string containing a JSON representation of an object. That’s what `jsonencode` does.

Have you tried `settings = jsonencode(each.value.settings)`? If not, please give it a shot and let me know what happens.

---

<div class="post-metadata">

**Author:** ![arcotek-ltd](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/arcotek-ltd/32/1674_2.png) [@arcotek-ltd](https://discuss.hashicorp.com/u/arcotek-ltd)\
**Post date:** [July 23, 2020, 7:53pm UTC](https://discuss.hashicorp.com/t/passing-json-literally-inside-a-string-literal/11859/4 "2020-07-23T19:53:55Z")

</div>

Thank you @alisdair, that’s just the ticket. You’re a lifesaver.

```auto
resource "azurerm_virtual_machine_extension" "pool" {
for_each = {
    for resource in local.ext_config :
    resource.id => resource
  }

type = each.value.type
# other attributes...

settings = jsonencode(each.value.settings)

}

```
