How to leverage JSON data in local file in my Alloy configuration?

So I am trying to learn how to use Alloy for the first time. One of the situations I am trying to figure out is, I have a date file (JSON) that I want to read some values from and be able to use that in my configuration.

For example, there are maybe half a dozen values that I’d essentially like to include as extra_labels for the loki_write component.

I did create a component for the local file just fine, and I can use the json_decode to get the data. But all I can figure out is:

external_labels = {
	platform = json_decode(local.file.app_info.content).platform,
	cluster = json_decode(local.file.app_info.content).cluster,
	environment  = json_decode(local.file.app_info.content).environment,
}

Is that really the way to do it? Or is there some better way to do the decoding and then leverage the map elsewhere in my config?

OK, I did some more noodling, and so far I have this -

local.file "app_info" {
	filename = "./app_info"
}

declare "app_info" {
	argument "input" {}

	export "data" {
		value = json_decode(argument.input.value)
	}
}

app_info "configuration" {
	input = local.file.app_info.content
}

That at least lets me get to values like app_info.configuration.data.<field>.
But what if I want to only export only specific properties from the JSON, and specify default values if they don’t exist?

2 Likes