0

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

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.