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
}