Search code examples
kubernetes-helm

Helm JSON being converted to map in configmap


I have an array of json objects like this in my values.yaml:

key: [{A:a, 1:2}, {B:b, 3:4}, {C:c, 5:6}]

when it gets moved to my config map it becomes:

key: [map[A:a 1:2] map[B:b 3:4] map[C:c 5:6]]

I'm not doing anything fancy with the template:

data:
  {{- range $key, $val := .Values.configs }}
  {{ $key }}: {{ $val | quote }}
  {{- end }}

So, how can I prevent helm from making that conversion with my JSON array?


Solution

  • In the form you have now, the value of key is an array of objects, and you're then getting the default Go serialization.

    If you instead want it to pass through exactly as that specific JSON string, you can quote it in your Helm values:

    key: '[{A:a, 1:2}, {B:b, 3:4}, {C:c, 5:6}]'
    #    ^  surround the string with quotes   ^
    

    Or, if you want to keep the values with that syntax (or a more complex YAML structure), Helm includes a toJson template function that can write out the structure as a JSON string:

    {{ $key }}: {{ $val | toJson | quote }}
    {{/*                  ^^^^^^       */}}