<p>I want to return a complex value (dict) from a Helm function, so that I can do further processing in the template or other functions.</p>
<p>I have defined this function:</p>
<pre><code>{{- define &quot;return-dict-function&quot; -}}
key1: value1
key2: value2
{{- end -}}
</code></pre>
<p>And I can output the function value in my template:</p>
<pre><code>{{ include &quot;return-dict-function&quot; . | nindent 2 }}
</code></pre>
<p>But how can I do further processing with the data?</p>

Matthias M
<p>There are several solutions to return complex values and do further processing:</p>
<p><strong>1) function returns plain yaml</strong></p>
<p>Take the example function <code>return-dict-function</code> from the question.
If you use <code>fromYaml</code> you'll get a <code>dict</code>:</p>
<pre><code>{{ $dict := include &quot;return-dict-function&quot; . | fromYaml }}
type: {{ $dict | typeOf }}
{{- range $key, $value := $dict }}
simple-function-{{ $key }} : processed-value-{{ $value }}
{{- end -}}
</code></pre>
<p>Output:</p>
<pre><code>type: map[string]interface {}
simple-function-key1 : processed-value-value1
simple-function-key2 : processed-value-value2
</code></pre>
<p><strong>2) function needs to return dict</strong></p>
<p><strong>a) serialize to json</strong></p>
<p>If you have a dict which should be returned you can serialize the dict with <code>toJson</code></p>
<pre><code>{{- define &quot;return-dict-function-json&quot; -}}
{{- $result := dict &quot;key1&quot; &quot;value1&quot; &quot;key2&quot; &quot;value2&quot; }}
{{- $result | toJson  -}}
{{- end -}}
</code></pre>
<p>Later you will deserialize with <code>fromJson</code></p>
<pre><code>{{ $dict := include &quot;return-dict-function-json&quot; . | fromJson }}
{{- range $key, $value := $dict }}
json-function-{{ $key }} : processed-value-{{ $value }}
{{- end -}}
</code></pre>
<p><strong>b) serialize to yaml</strong></p>
<p>You can also serialize the dict with <code>toYaml</code></p>
<pre><code>{{- define &quot;return-dict-function-yaml&quot; -}}
{{- $result := dict &quot;key1&quot; &quot;value1&quot; &quot;key2&quot; &quot;value2&quot; }}
{{- $result | toYaml  -}}
{{- end -}}
</code></pre>
<p>Then you need to deserialize with <code>fromYaml</code></p>
<pre><code>{{ $dict := include &quot;return-dict-function-yaml&quot; . | fromYaml }}
{{- range $key, $value := $dict }}
yaml-function-{{ $key }} : processed-value-{{ $value }}
{{- end -}}
</code></pre>
<p><strong>Notes and further readings</strong></p>
<ul>
<li>Of course this also works with nested values</li>
<li><a href="https://blog.flant.com/advanced-helm-templating/" rel="nofollow noreferrer">Advanced Helm Templating</a></li>
<li><a href="https://helm.sh/docs/chart_template_guide/function_list/" rel="nofollow noreferrer">Helm Docs</a></li>
</ul>
