<p>Assuming the following JSON:</p>
<pre class="lang-json prettyprint-override"><code>{
   &quot;A&quot;:{
      &quot;A_KEY&quot;:&quot;%PLACEHOLDER_1%&quot;,
      &quot;B_KEY&quot;:&quot;%PLACEHOLDER_2%&quot;
   }
}
</code></pre>
<p>And, the following values.yaml:</p>
<pre class="lang-yaml prettyprint-override"><code>placeholders:
  PLACEHOLDER_1: Hello
  PLACEHOLDER_2: World
</code></pre>
<p>I would like to load this JSON using Configmap into my pod. But, replace the placeholders with the values under <code>values.yaml</code> <strong>automatically based on the key</strong>.
Thought about writing a simple regex which will search for the words between two <code>%</code> and use this word with <code>.Values.placeholders.$1</code>.</p>
<p>So far, I managed to replace single value using:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: config
data:
  config.json: |-
    {{- regexReplaceAll &quot;%PLACEHOLDER_1%&quot; (  .Files.Get &quot;config.json&quot;) .Values.placeholders.PLACEHOLDER_1 | nindent 4 }}
</code></pre>
<p>The final goal is to replace both PLACEHOLDER_1 and PLACEHOLDER_2 by single regex.</p>
<p>desired JSON:</p>
<pre class="lang-json prettyprint-override"><code>{
   &quot;A&quot;:{
      &quot;A_KEY&quot;:&quot;Hello&quot;,
      &quot;B_KEY&quot;:&quot;World&quot;
   }
}
</code></pre>
<p>Any help will be much appriciated.</p>

<p>Here is what I have come up with:</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: config
data:
  config.json: |-
    {{- $file := .Files.Get &quot;config.json&quot; }}
    {{- range $k, $v := .Values.placeholders }}
      {{- $file = regexReplaceAll (printf &quot;%%%s%%&quot; $k) $file  $v }}
    {{- end }}
    {{- print $file | nindent 4 }}
</code></pre>
<p>I load a config file content into a <code>$file</code> variable and then iterate over all placeholers.keys from values.yaml file and replace them one by one, saving the output back to the same variable. At the end, <code>$file</code> variable has all fields substituted so I just print it.</p>
