<p>I am trying to patch a secret using kubectl</p>
<pre><code>kubectl patch secret operator-secrets --namespace kube-system --context=cluster1 --patch &quot;'{\&quot;data\&quot;: {\&quot;FOOBAR\&quot;: \&quot;$FOOBAR\&quot;}}'&quot;
</code></pre>
<p>But I receive the error</p>
<blockquote>
<p>Error from server (BadRequest): json: cannot unmarshal string into Go value of type map[string]interface {}</p>
</blockquote>
<p>If I run the command using echo, it seems to be a valid JSON</p>
<pre><code>$ echo &quot;'{\&quot;data\&quot;: {\&quot;FOOBAR\&quot;: \&quot;$FOOBAR\&quot;}}'&quot;

'{&quot;data&quot;: {&quot;FOOBAR&quot;: &quot;value that I want&quot;}}'
</code></pre>
<p>What can be?</p>

<blockquote>
<p>If I run the command using echo, it seems to be a valid JSON</p>
</blockquote>
<p>In fact, it does not. Look carefully at the first character of the output:</p>
<pre><code>'{&quot;data&quot;: {&quot;FOOBAR&quot;: &quot;value that I want&quot;}}'
</code></pre>
<p>Your &quot;JSON&quot; string starts with a single quote, which is an invalid character. To get valid JSON, you would need to rewrite your command to look like this:</p>
<pre><code>echo &quot;{\&quot;data\&quot;: {\&quot;FOOBAR\&quot;: \&quot;$FOOBAR\&quot;}}&quot;
</code></pre>
<p>And we can confirm that's valid JSON using something like the <code>jq</code>
command:</p>
<pre><code>$ echo &quot;{\&quot;data\&quot;: {\&quot;FOOBAR\&quot;: \&quot;$FOOBAR\&quot;}}&quot;   | jq .
{
  &quot;data&quot;: {
    &quot;FOOBAR&quot;: &quot;value that i want&quot;
  }
}
</code></pre>
<p>Making your patch command look like:</p>
<pre><code>kubectl patch secret operator-secrets \
  --namespace kube-system \
  --context=cluster1 \
  --patch &quot;{\&quot;data\&quot;: {\&quot;FOOBAR\&quot;: \&quot;$FOOBAR\&quot;}}&quot;
</code></pre>
<p>But while that patch is now valid JSON, it's still going to fail with
a new error:</p>
<pre><code>The request is invalid: patch: Invalid value: &quot;map[data:map[FOOBAR:value that i want]]&quot;: error decoding from json: illegal base64 data at input byte 5
</code></pre>
<p>The value of items in the <code>data</code> map must be base64 encoded values.
You can either base64 encode the value yourself:</p>
<pre><code>kubectl patch secret operator-secrets \
  --namespace kube-system \
  --context=cluster1 \
  --patch &quot;{\&quot;data\&quot;: {\&quot;FOOBAR\&quot;: \&quot;$(base64 &lt;&lt;&lt;&quot;$FOOBAR&quot;)\&quot;}}&quot;
</code></pre>
<p>Or use <code>stringData</code> instead:</p>
<pre><code>kubectl patch secret operator-secrets \
  --namespace kube-system \
  --context=cluster1 \
  --patch &quot;{\&quot;stringData\&quot;: {\&quot;FOOBAR\&quot;: \&quot;$FOOBAR\&quot;}}&quot;
</code></pre>
