Extract key value from json and populate list in Terraform

Hi @rbankole,

I’m not sure I fully understood your goal but I think you want to find a list of just the Topic property values from those event objects. The JSON you shared doesn’t seem complete but assuming that the JSON file contains a top-level JSON object where Events is one of its properties then you could do that using something like the following:

locals {
  raw_data     = jsondecode(file("${path.module}/example.json"))
  event_topics = local.raw_data.Events[*].Topic
}

local.raw_data is a representation of the contents of that JSON file in the Terraform type system. Because the JSON file contains a JSON object, the result is a Terraform object value.

We can then use Terraform expressions to work with that object value. In local.event_topics I used a splat expression to concisely select the Topic attribute from each of the nested objects in the local.raw_data.Events sequence.

2 Likes