0

let's suppose we have this variable:

  foobars = {
    "first" : {
      specialkeys: [
        "a",
        "b",
        "c"
      ]
    }
    "second" : {}
  }

now let's say we would like to loop over that foobars object knowing that specialkeys doesn't exist in the "second" object.

This is what I tried but it complains that

This object does not have an attribute named specialkeys

My tries:

  data = flatten([
    for k, v in var.foobars : [
      for sk in v.specialkeys : {
          special = sk,
          foo = k
      }
    ]
  ])
A Akrm
  • 97
  • 7

1 Answers1

0

I believe you would want to do the following:

data = flatten([
    for k, v in local.foobars :
    [
      for sk, sv in v :
      [
        for spec in sv : {
          special = spec,
          foo     = k
        }
      ]
    ]
  ])

This will output something like this:

[
  {
    "foo" = "first"
    "special" = "a"
  },
  {
    "foo" = "first"
    "special" = "b"
  },
  {
    "foo" = "first"
    "special" = "c"
  },
]
Ervin Szilagyi
  • 14,274
  • 2
  • 25
  • 40