How to generate Helm charts config file with JSON data

7/10/2019

I want to generate complex JSON with HELM template

I have a template:

apiVersion: v1
kind: ConfigMap
metadata:
  name: test
data:
  config.json: |
  {
    "test": "{{ $Value }}",
    "services": {
      {{- range $k, $v := $.Values.services }}
      "{{ $k | upper }}_PATH": "{{ $k }}",
      {{- end }}
     }
  }

Helm chars generates json:

{
  "test": "test",
  "services": {
     "S1_PATH": "/t1",
     "S2_PATH": "/t2",
     "S2_PATH": "/t3",
   }
}

Problem is that JSON is not valid as it has a trailing comma. How to update the template to resolve this?

-- Mindaugas Jaraminas
json
kubernetes
kubernetes-helm

2 Answers

7/11/2019

Maybe I dont undersood your question correctly, but to avoid trailing comma, you should remove it from your template. Plus I see you should use .Values.services instead of $.Values.services

template:

apiVersion: v1
kind: ConfigMap
metadata:
  name: test
data:
  config.json: |
  {
    "services": {
      {{- range $k, $v := .Values.services }}
      "{{ $k | upper }}_PATH": "{{ $k }}"
      {{- end }}
     }
  }

result:

apiVersion: v1
kind: ConfigMap
metadata:
  name: test
data:
  config.json: |
  {
    "services": {
      "S1_PATH": "s1"
      "S2_PATH": "s2"
      "S3_PATH": "s3"
     }
  }

If this i snot you expected - please add more info in your question.

-- VKR
Source: StackOverflow

11/5/2019

If you update the template like this, it should work.

apiVersion: v1
kind: ConfigMap
metadata:
  name: test
data:
  config.json: |
  {
    "test": "{{ $Value }}",
    "services": {
      {{- range $k, $v := $.Values.services }}
      {{ if ne $k 0 }},{{ end }}
      "{{ $k | upper }}_PATH": "{{ $k }}"
      {{- end }}
     }
  }

For the first service (index is 0) it does not place a comma and for all others it places one in front.

-- mpoqq
Source: StackOverflow