I have this json file (map.json):
[
{
"QueueName": "queue1",
"TypeName": "type1"
},
{
"QueueName": "queue2",
"TypeName": "type2"
},
{
"QueueName": "queue3",
"TypeName": "type2"
}
]
which I can load into the following variable:
locals {
maps = jsondecode(file("maps.json"))
}
How can I read the TypeName value for QueueName = "queue2"?
asked Nov 10, 2022 at 16:22
romanoza
4,9773 gold badges29 silver badges45 bronze badges
1 Answer 1
I think you are looking at something like this:
type = [for el in local.maps : el["TypeName"] if el["QueueName"] == "queue2"]
Output
$ terraform console
> local.response
[
"type1",
]
Logically this will return a list of elements, but if you want to retrieve only the first result, then you can use:
response = [for el in local.maps : el["TypeName"] if el["QueueName"] == "queue1"]
type = length(local.response) > 0 ? local.response[0] : ""
Outputs
$ terraform console
> local.type
"type1"
>
Or just:
type = local.maps[index(local.maps.*.QueueName, "queue1")]["TypeName"]
But this will throw an exception if the element queueName does not exist, something like this:
Call to function "index" failed: item not found
answered Nov 10, 2022 at 19:05
Youcef LAIDANI
60.3k21 gold badges112 silver badges178 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default