values.yaml:
products:
apples:
- green
- red
- yellow
I want to convert this into JSON file in chart:
"products": {
"apples": [
"green",
"red",
"yellow"
]
},
In jinja2 it would look like something like this:
"products": {
"apples": [
{% for apple in products.apples%}
"{{apple}}"{% if not loop.last %},{% endif %}
{% endfor %}
]
},
In jinja2 loop.last is used. How it can be done in helm?
The easiest answer to your real question is to use the Helm toJson
function.
"products": {
{{ toPrettyJson .Values.products | indent 2 }}
}
To do this by hand: in the core Go text/template
language, you can assign temporary variables as you loop over a structure with range
. There's a two-variable form that receives both the index and the value. You can then compare the index with the length of the list.
"products": {
"apples": {
{{- $last := sub (len .Values.products.apples) 1 }}
{{- $index, $apple := range .Values.products.apples }}
{{ quote $apple }}{{ if ne $index $last }},{{ end }}
{{- end }}
}
}