Conversion of map to object

Hi All,

I was reading the terraform documentation Conversion of Complex Types and i was trying to understand the points on object to map and map to object

(Values with additional keys can still match an object type, but the extra attributes are discarded during type conversion.)

I tried to create an example on this like if i have an object and i have mentioned {k1=v,k2=v,k3=v} ; it will accept only 3 {k,v} and if provide more than 3 it will discard

On the other hand

  • A map (or a larger object) can be converted to an object if it has at least the keys required by the object schema. Any additional attributes are discarded during conversion, which means map → object map conversions can be lossy.
 cat map_demo.tf
variable "map_demo_1" {
description="demo on map(string)"
type=map(any)
}

output "map_string_output" {
value=var.map_demo_1
}
terraform plan
var.map_demo_1
  demo on map(string)

  Enter a value: {name="Aniket Pant",age=28,status=true,gender="M"}


Changes to Outputs:
  + map_string_output = {
      + age    = "28"
      + gender = "M"
      + name   = "Aniket Pant"
      + status = "true"
    }

You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.

I wanted to test for map to object where conversions can be lossy .

1 Like

Anyone know on this or my question is wrong.

I am closing this thread as i raised this question on github and my doubt is solved Conversion of map to object · Issue #36256 · hashicorp/terraform · GitHub

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.