# Using variable to access attribute of nested variable

**URL:** <https://discuss.hashicorp.com/t/using-variable-to-access-attribute-of-nested-variable/38354>\
**Category:** Terraform\
**Created:** [April 14, 2022, 1:03pm UTC](https://discuss.hashicorp.com/t/using-variable-to-access-attribute-of-nested-variable/38354 "2022-04-14T13:03:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lukopas](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/lukopas/32/24120_2.png) [@lukopas](https://discuss.hashicorp.com/u/lukopas)\
**Post date:** [April 14, 2022, 1:03pm UTC](https://discuss.hashicorp.com/t/using-variable-to-access-attribute-of-nested-variable/38354/1 "2022-04-14T13:03:56Z")

</div>

I want to do something like this, but take the values from variables.

This works:

locals {

map = {  
val1 = [  
{  
somedata = “”  
}  
],  
val2 = [  
{  
somedata = “”  
}  
]  
}

test = [for obj in local.map: try(obj.val1,"") if try(obj.val1,"")!=""]

}

But how can I do this taking val1 or val2 from the variables:  
These don’t work  
test = [for obj in local.map: try(obj.(var.containsval1),"") if try(obj.(var.containsval1),"")!=""]  
or smth like this  
test = [for obj in local.map: try(obj."${var.containsval1}","") if try(obj."${var.containsval1}","")!=""]

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [April 14, 2022, 3:15pm UTC](https://discuss.hashicorp.com/t/using-variable-to-access-attribute-of-nested-variable/38354/2 "2022-04-14T15:15:43Z")

</div>

Hi @lukopas,

I’m not sure I fully understand what you are asking with such a contrived example but if I _am_ understanding correctly I think you are looking for the “indexing” syntax, which allows looking up an element of a collection or attribute of an object using the value of some other arbitrary expression:

```auto
var.map[var.key]

```

The above, assuming that `var.map` is a map and `var.key` is a string, will return the value of the element of the map whose key is equal to `map.key`, or raise an error if there is no such element. You can then optionally combine that with the `try` function if you want to react to that error by returning some other placeholder value instead.

---

<div class="post-metadata">

**Author:** ![lukopas](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/lukopas/32/24120_2.png) [@lukopas](https://discuss.hashicorp.com/u/lukopas)\
**Post date:** [April 15, 2022, 7:14am UTC](https://discuss.hashicorp.com/t/using-variable-to-access-attribute-of-nested-variable/38354/3 "2022-04-15T07:14:17Z")

</div>

Data is in JSON file that I don’t see, it looks like this  
[  
{  
“somevalue”: [  
{  
“stuff”: “data”,  
},  
{  
“stuff”: “data”,  
},  
{  
“stuff”: “data  
}  
]  
},  
{  
“someothervalue”: [  
{  
“stuff”: “data”,  
},  
{  
“stuff”: “data”,  
},  
{  
“stuff”: “data  
}  
]  
}  
I need to do this (extract specific part of this json)  
map= try(jsondecode(file(try(”…/terraform/configs/settings\_new.json”,""))), {})  
test = [for obj in local.map: try(obj.somevalue,"") if try(obj.somevalue,"")!=""]

But As i don’t know if it is somevalue or someohervalue or something else. I have it in variable

variable “CONFIG\_INDEX” {  
description = “value to extract string”  
}  
SO lets us say that CONFIG\_INDEX = somevalue  
So my code should look like this but it oesn’t work:  
map= try(jsondecode(file(try("…/terraform/configs/settings\_new.json",""))), {})  
test = [for obj in local.map: try(obj.(var.CONGIF\_INDEX),"") if try(obj.(var.CONFIG\_INDEX),"")!=""]

How can I do something like this?

---

<div class="post-metadata">

**Author:** ![lukopas](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/lukopas/32/24120_2.png) [@lukopas](https://discuss.hashicorp.com/u/lukopas)\
**Post date:** [April 15, 2022, 7:42am UTC](https://discuss.hashicorp.com/t/using-variable-to-access-attribute-of-nested-variable/38354/4 "2022-04-15T07:42:03Z")

</div>

Ok I accidentally found a way

To access variable value as dot attribute you can do this

test = [for obj in local.map: try(obj[var.CONGIF\_INDEX],"") if try(obj[var.CONFIG\_INDEX],"")!=""]

THERE MUST BE NO DOT after loop element, otherwise it doesn’t work.

LoopElement[var.VariableContainingValue]
