Merging two maps of objects

Hi guys,

I’m looking for some advice or help with the following. I’m not even sure if it’s possible.

locals {
   objs_a = {
       "item_1" = {
            "field1" = "value"
            "field2" = [ 
                 "list1", "list2"
            ]
       } 
   }
   objs_b = {
       "item_1" = {
            "field1" = ""
            "field2" = [ 
                 "list3"
            ]
       } 
   }
   objs_c = {
       "item_1" = {
            "field1" = "value"
            "field2" = [ 
                 "list1"
                 "list2"
                 "list3"
            ]
       } 
   }
}

So, I have two maps of object(string, map(string)) that I want to merge, such that in the example above, merge(local.objs_a, local.objs_b) = local.objs_c.

From what I can tell in the merge docs, when both maps contain the same key, it just takes the value from the later specified map. I want to make it merge two items together, null coalescing the string fields and merging the list fields.

Is this even possible, and if so, how?

1 Like

did you ever find a solution?

I am looking for a workaround for similar issue.

I have two maps with one common key .

Code

locals {
map1 = {
“key1” = “value1”,
“key2” = “value2”
}
map2 = {
“key1” = “value11”
}
}

output MergedMaps {
value = merge(local.map1, local.map2)
}

Output

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

MergedMaps = {
  "key1" = "value11"
  "key2" = "value2"
}

The desired output

merged_map = {
“key1” = [“value1”, “value11”]
“key2” = “value2”
}

Thanks for help