Reading element in harness provider block

64 Views Asked by At

I have following json

 {
   "applications": [
     {"abc": {"app": "myapp", "org": "myorg_a"}},
     {"bcd": {"app": "myapp2", "org": "myorg_b"}}
   ]
 }

I need org = "myorg_a" in harness provider block https://registry.terraform.io/providers/harness/harness/latest/docs

#Configure the Harness provider for Next Gen resources
 provider "harness" {
    endpoint         = "https://app.harness.io/gateway"
    account_id       = myorg_a
    platform_api_key = "mike"
 }

I need to read myorg_a dynamically since tomorrow json may change to myorg_c. I need help to achieve it.

Using (data.http.myapi.body).applications.abc.org I can read myorg_a but can't hardcode abc.

When I tried (data.http.myapi.body).applications[0]["org"] I get error "The given key does not identify an element in this collection value. An object only supports looking up attributes by name, not by numeric index."

1

There are 1 best solutions below

0
On

I created this proposal using local variable and flattened that object using a nested iteration.

locals {
  data =  {
   "applications": [
     {"abc": {"app": "myapp", "org": "myorg_a"}},
     {"bcd": {"app": "myapp2", "org": "myorg_b"}}
   ]
 }
 data_org = flatten([ for k,v in local.data["applications"] : [
         for x,z in v :[
          {org = z.org}
         ]
  
 ]])

}

output for testing

output "get_values" {
  value = local.data_org
}

enter image description here