M. Buil
M. Buil

Reputation: 569

How to fetch a value inside an array using helm template

I have the following structure:

label:
  foo:
    bar:
    - x: hello
      y: hallo
      z: hola

In order to reach the value of z, I am currently doing:

{{ $bar := pick .Values.label.foo "bar" }}
{{ $firstItem := first $bar }}
{{ $myValue := get $firstItem "z" }}

Is there a more concise way to do this? I tried something like pick .Values.label.foo[0].z but that does not work

Upvotes: 0

Views: 3107

Answers (1)

David Maze
David Maze

Reputation: 159515

Since the values structure you show is just simple string-keyed dictionaries and lists, you don't need functions like pick or get; you can just use the . operator to retrieve a specific key from the containing dictionary.

{{ $bar := .Values.label.foo.bar }}
{{ $firstItem := first $bar }}
{{ $myValue := $firstItem.z }}

Then you can replace the variable references with their expressions, using (...) parenthesis grouping if needed.

{{ $myValue := (first .Values.label.foo.bar).z }}

You can also use the standard index function here; I believe it is legal to mix array and map keys. This will give you a single call, though a mix of indexing syntax.

{{ $myValue := index .Values.label.foo.bar 0 "z" }}
{{ $myValue := index .Values "label" "foo" "bar" 0 "z" }}

Upvotes: 3

Related Questions