# How to merge two dynamically generated maps

**URL:** <https://discuss.hashicorp.com/t/how-to-merge-two-dynamically-generated-maps/34919>\
**Category:** Terraform\
**Created:** [January 28, 2022, 3:16pm UTC](https://discuss.hashicorp.com/t/how-to-merge-two-dynamically-generated-maps/34919 "2022-01-28T15:16:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsantanu](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/dsantanu/32/1585_2.png) [@dsantanu](https://discuss.hashicorp.com/u/dsantanu)\
**Post date:** [January 28, 2022, 3:16pm UTC](https://discuss.hashicorp.com/t/how-to-merge-two-dynamically-generated-maps/34919/1 "2022-01-28T15:16:55Z")

</div>

I have a map like this:

```auto
vault_secrets:
  path1:
    - audit_db_service
    - sys_admin_role
  path2:
    - mis
    - jml_snow_service

```

and from there, I like to have a resultant map, like:

```auto
{
  "path1/audit_db_service" = "auditdb"
  "path1/sys_admin_role" = "sysadmin"
  "path2/mis" = "mis"
  ....
}

```

I came up with this:

```auto
[
   for K1,V1 in var.vault_secrets : merge({
      for v2 in V1 : "${K1}/${v2}" => join(
        "", slice(split("_",v2),0,length(
          split("_",v2))>1 ? (length(split("_",v2))-1) : 1
        )
      )
   })
]

```

which, gives me this result:

```auto
 [
  {
    "path1/audit_db_service" = "auditdb"
    "path1/sys_admin_role" = "sysadmin"
  },
  {
    "path2/jml_snow_service" = "jmlsnow"
    "path2/mis" = "mis"
  },
]

```

and that’s where I got stuck. Can anyone give me a git of hands pls?  
Also, any idea why that `merge(...)` in my code doesn’t work?

-S

---

<div class="post-metadata">

**Author:** ![tbugfinder](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/tbugfinder/32/12689_2.png) [@tbugfinder](https://discuss.hashicorp.com/u/tbugfinder)\
**Post date:** [January 28, 2022, 9:18pm UTC](https://discuss.hashicorp.com/t/how-to-merge-two-dynamically-generated-maps/34919/2 "2022-01-28T21:18:59Z")

</div>

try this with your input variable:

```auto
locals {
  myres = [
    for K1, V1 in var.vault_secrets : merge({
      for v2 in V1 : "${K1}/${v2}" => join(
        "", slice(split("_", v2), 0, length(
          split("_", v2)) > 1 ? (length(split("_", v2)) - 1) : 1
        )
      )
    })
  ]

  myres2 = merge([for k1, v1 in var.vault_secrets : { for k2 in v1 : "${k1}/${k2}" => join(
    "", slice(split("_", k2), 0, length(
      split("_", k2)) > 1 ? (length(split("_", k2)) - 1) : 1
  )) }]...)

}

variable "vault_secrets" {
  type = map(list(string))
  default = { path1 = ["audit1", "sys_admin"], path2 = ["mis", "jml_snow"] }
}

output "result" {
  value = local.myres
}
output "result2" {
  value = local.myres2
}

```

---

<div class="post-metadata">

**Author:** ![tbugfinder](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/tbugfinder/32/12689_2.png) [@tbugfinder](https://discuss.hashicorp.com/u/tbugfinder)\
**Post date:** [January 28, 2022, 9:19pm UTC](https://discuss.hashicorp.com/t/how-to-merge-two-dynamically-generated-maps/34919/3 "2022-01-28T21:19:28Z")

</div>

```auto
% terraform plan

Changes to Outputs:
  + result = [
      + {
          + path1/audit1 = "audit1"
          + path1/sys_admin = "sys"
        },
      + {
          + path2/jml_snow = "jml"
          + path2/mis = "mis"
        },
    ]
  + result2 = {
      + path1/audit1 = "audit1"
      + path1/sys_admin = "sys"
      + path2/jml_snow = "jml"
      + path2/mis = "mis"
    }

```

---

<div class="post-metadata">

**Author:** ![dsantanu](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/dsantanu/32/1585_2.png) [@dsantanu](https://discuss.hashicorp.com/u/dsantanu)\
**Post date:** [January 28, 2022, 10:38pm UTC](https://discuss.hashicorp.com/t/how-to-merge-two-dynamically-generated-maps/34919/4 "2022-01-28T22:38:02Z")

</div>

thanks @tbugfinder !!  
Looks like that’s all what I wanted but let me try and confirm.
