Tranposing a Map of Maps

Hi,

I’ve got a map of Azure AD users (data.azuread_user) which looks like a consecutive bunch of these:

"XXXXX-XXXXX-XXXXX-XXXXX" = {
    "account_enabled" = true
    "display_name" = "Poppa Smurf"
    "id" = "XXXXX-XXXXX-XXXXX-XXXXX"
    "immutable_id" = ""
    "mail" = "poppa.smurf@company.com"
    "mail_nickname" = "poppa.smurf"
    "object_id" = "XXXXX-XXXXX-XXXXX-XXXXX"
    "onpremises_sam_account_name" = ""
    "onpremises_user_principal_name" = ""
    "usage_location" = "US"
    "user_principal_name" = "poppa.smurf@company.com"
  }    

Due to the way I generated it, via a for_each off the list of members on the all users group in Azure AD, the key is the UUID.

How can I transpose or otherwise munge the list so that the mail_nickname subvalue becomes the top level key instead? I’ve look at transpose and “for blah in blah” but can’t work out how to put either of those together to do the thing.

End result will hopefully be:

"poppa.smurf" = {
    "account_enabled" = true
    "display_name" = "Poppa Smurf"
    "id" = "XXXXX-XXXXX-XXXXX-XXXXX"
    "immutable_id" = ""
    "mail" = "poppa.smurf@company.com"
    "mail_nickname" = "poppa.smurf"
    "object_id" = "XXXXX-XXXXX-XXXXX-XXXXX"
    "onpremises_sam_account_name" = ""
    "onpremises_user_principal_name" = ""
    "usage_location" = "US"
    "user_principal_name" = "poppa.smurf@company.com"
  }  

Cheers,

Hi @brendan-sherrin,

A for expression does seem to me like the most direct way to get the result you want. Here’s an example for expression to change to a different set of keys:

{ for v in data.azuread_user.example : v.mail_nickname => v }

Does that seem like the sort of thing you were looking for? If not, it’d be helpful to see some examples of what you tried already and how their results differed from what you were intending. Thanks!

1 Like

Hi,
Thanks for the help, using for worked a treat.

I ended up cleaning it up as below, also discovered that I should use UPN as the key and not mail as not all Azure AD users will have a mail, but all have a UPN. I had blanks in a later lookup.

for UUID in data.azuread_user.azuread_user :

          UUID.user_principal_name => {

              account_enabled = UUID.account_enabled

              id = UUID.id

              mail = UUID.mail

              mail_nickname = UUID.mail_nickname

              usage_location = UUID.usage_location

              user_principal_name = UUID.user_principal_name

          }