Flatten loops issue

using below json file for my code,

[
{
“teams”:
[
{
“teamname”: “teamname1”,
“sets”:
[
{
“setid” : “set1”,
“setname” :“set1name”,
“subsets”: [
{
“subsetid”: “set1-subset1”,
“subsetname”: “set1-subsetname1”
},
{
“subsetid”: “set1-subset2”,
“subsetname”: “set1-subsetname2”
}
]
},
{
“setid” : “set2”,
“setname” :“set2name”,
“subsets”: [
{
“subsetid”: “set2-subset1”,
“subsetname”: “set2-subsetname1”
},
{
“subsetid”: “set2-subset2”,
“subsetname”: “set2-subsetname2”
}
]
}
]
}
]
}
]

I need to flatten it as team-set1-subset1…
in my output

Not 100% clear what output you want, but a list of strings created by concatenating the value of teamname+subsetid:

Outputs:
thing_output = [
  "teamname1-set1-set1-subset1",
  "teamname1-set1-set1-subset2",
  "teamname1-set2-set2-subset1",
  "teamname1-set2-set2-subset2",
]

Is produced by this code:

locals {
  # The originally provided JSON
  json_input = <<EOT
 [ 
  {
    "teams": [
      {
        "teamname": "teamname1",
        "sets": [
          {
            "setid": "set1",
            "setname": "set1name",
            "subsets": [
              {
                "subsetid": "set1-subset1",
                "subsetname": "set1-subsetname1"
              },
              {
                "subsetid": "set1-subset2",
                "subsetname": "set1-subsetname2"
              }
            ]
          },
          {
            "setid": "set2",
            "setname": "set2name",
            "subsets": [
              {
                "subsetid": "set2-subset1",
                "subsetname": "set2-subsetname1"
              },
              {
                "subsetid": "set2-subset2",
                "subsetname": "set2-subsetname2"
              }
            ]
          }
        ]
      }
    ]
  }
]

EOT

  # Decode JSON
  decoded = jsondecode(local.json_input)

  # Create the List of Strings
  thing = flatten([ for toplevel in local.decoded :[
    for teamn, teamv in toplevel.teams : [
    for setn, setv in teamv.sets : [
      for subsetn, subsetv in setv.subsets :
      "${teamv.teamname}-${setv.setid}-${subsetv.subsetid}"
    ]
    ]
  ]])
}

output "thing_output" {
  value = local.thing
  description = "Output of thing"
}

Hope that helps

To flatten the JSON structure you’ve provided into a format like team-set-subset, you can use a Terraform configuration with nested for loops. The goal is to iterate over each level of the structure—teams, sets, and subsets—to construct the desired string format.

Here’s an example Terraform code snippet that demonstrates how to accomplish this. This example assumes you have the JSON data available as a file or a variable within Terraform.

First, ensure your JSON data is correctly formatted (the example you provided contains some typographical issues like “” instead of “”) and saved in a file, let’s say data.json.

hclCopy code

locals {
  # Load and parse the JSON data
  json_data = jsondecode(file("${path.module}/data.json"))

  # Flatten the structure into the desired format
  flattened = flatten([
    for team in local.json_data : [
      for set in team.teams[0].sets : [
        for subset in set.subsets : "${team.teams[0].teamname}-${set.setid}-${subset.subsetid}"
      ]
    ]
  ])
}

output "flattened_structure" {
  value = local.flattened
}