<p>I have a json file with some keys like this:</p>
<pre><code>{
  &quot;a&quot;:&quot;someval&quot;
  &quot;b&quot;:&quot;someval&quot;
  .. more keys
}
</code></pre>
<p>How do I add these keys to a secret in kubernetes?</p>
<p>When I try <code>$ kubectl create secret generic mysecret --from-file=file.json</code> it returns a secret containing the file, but <strong>I want to map the contents of the file to the secret</strong>, not add the file as a secret.</p>
<p>Output:</p>
<p><code>$ kubectl get secret mysecret -o yaml</code></p>
<pre><code>apiVersion: v1
data:
  file.json: #base64 encoded stuff here.
kind: Secret
</code></pre>
<hr />
<p><strong>Wanted output:</strong></p>
<p><code>$ kubectl get secret mysecret -o yaml</code></p>
<pre><code>apiVersion: v1
data:
  a: someval
  b: someval
kind: Secret
</code></pre>
<p>What am I doing wrong?</p>

<p>If you have flat (not nested) JSON then try this (assuming you have <a href="https://stedolan.github.io/jq/download/" rel="noreferrer"><code>jq</code></a> tool installed):</p>

<pre><code>kubectl create secret generic test --from-env-file &lt;(jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]" YOUR_FILE.json)
</code></pre>
