Terraform Merge two list with identical structure [key]

Need help on this : How to merge two identical - id & Values [App Role & Oauth scope] for same app.

Any help is appreciated !!!

oauth_merge_permissions  = [
       {
           App1 = {
               role1 = "729e1819-d582-d503-deff-2be33394cee5"
               role2     = "fb9b9a2d-2653-ad21-556b-67b40742f4e1"
               role3    = "f9dbd854-e42e-298e-c7d6-ba068d67261d"
               role4   = "5324f064-ac54-db19-16c0-0510a3b979dc"
               role5  = "d1577294-3870-e465-c433-e036bd2d624c"
               role6     = "c13d1bc7-ffc2-dd8c-001e-c42c7c1b32cc"
            }
        },
       {
           App1 = {
               test               = "da2a01d8-9865-412b-b7ef-f48f1e56e481"
               user_impersonation = "7b31ca60-1333-4620-9582-012f25f4da05"
            }
        },
        {
           App2 = {
               role5  = "24342dsfdg434-45-e465-c433-e036b1d322"
               role6     = "saas23dfg5-1333-4620-9582-012f25f4da05"
            }
        }
    ]

Desired Output: I would like to get below output so that i can able to utilize this output in azuread_application API permission.

Result = [
       {
           App1 = {
               role1 = "729e1819-d582-d503-deff-2be33394cee5"
               role2     = "fb9b9a2d-2653-ad21-556b-67b40742f4e1"
               role3    = "f9dbd854-e42e-298e-c7d6-ba068d67261d"
               role4   = "5324f064-ac54-db19-16c0-0510a3b979dc"
               role5  = "d1577294-3870-e465-c433-e036bd2d624c"
               role6     = "c13d1bc7-ffc2-dd8c-001e-c42c7c1b32cc"
               test               = "70efda02-14a2-48f2-9297-8d86e7737438"
               user_impersonation = "e100b04b-fb46-b764-8ae9-513e1f1cd469"
            }
        },       
        {
           App2 = {
               role5  = "24342dsfdg434-45-e465-c433-e036b1d322"
               role6     = "saas23dfg5-1333-4620-9582-012f25f4da05"
            }
        }
    ]

Merging nested structures like this can be a bit cumbersome right now, but I think here is something that can hopefully get you moving:

 value = { for k, _ in merge(local.oauth_merge_permissions...) :
    k => merge([for obj in local.oauth_merge_permissions : 
      obj[(k)] if lookup(obj, (k), null) != null]...)
  }

What this does is merge once to collect the top level key names, but since merge is not recursive, the values are not going to be correct. Once we have the key names though, we use those to filter the original list and merge the nested objects.

1 Like

Thanks so much @jbardin , much appreciated for the effort !! this approach worked for my scenario i was stuck on this for past couple of days , you saved me :slight_smile: !!!

Glad it worked out for you @mukeshinit!

Replying here because I for some reason left some unnecessary parentheses in the example, and while it doesn’t change the operation, it makes the example noisier and may lead some users to think they are required:

 value = { for k, _ in merge(local.oauth_merge_permissions...) :
    k => merge([for obj in local.oauth_merge_permissions : 
      obj[k] if lookup(obj, k, null) != null]...)
  }
1 Like

Thanks again for the explanation!!!