Terraform type returned by jsondecode

What type can I use for a module receiving the output of jsondecode?

module "workspaces" {
  source = "../../"
  variables        = jsondecode(file("dev.tfvars.json"))
}

At the moment I using any but is there is a correct type to use?

variable "variables" {
  type          = any
  default     = null
}

Hi @deasunk,

The result type of jsonencode depends on what JSON value is in your dev.tfvars.json file, as described in the type mapping table in the jsondecode documentation.

The type constraint any will accept a value of any type, but I expect your module is expecting a particular data structure and so it would be better to describe what that data structure is explicitly so that Terraform can automatically check it. If your JSON file contains a JSON object with some properties then the most direct corresponding Terraform type would be an object type, like this:

variable "variables" {
  type = object({
    environment_name = string
    global_tags      = map(string)
    # etc, whatever attributes the module expects
  })
}

When deciding this I would encourage you to think less about what the JSON file currently contains and more about what data structure best describes what the module needs from this variable value. You can then either make the JSON file contents conform to the type constraint, or you can write an expression to explicitly convert from the structure inside the JSON file to the structure that the module expects.