Home > other >  Using toJson or toRawJson without having it automatically adding quotation marks?
Using toJson or toRawJson without having it automatically adding quotation marks?

Time:12-22

So I have a values.yaml file with an string variable representing a database connection string with no quotes looking like this (don't worry, not the real password):

ActionLogsConnectionString: Database=ActionLogs;Server=SQL_DEV;User Id=sa;Password=Y19|yx\dySh53&h

My goal is to print it inside a ConfigMap resource so that it can then be injected in my pod as a .json configuration file for a dotnet app. I also want to append the application name in the connection string:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "asp.fullname" . }}
  labels:
    {{- include "asp.labels" . | nindent 4 }}
data:
  appsettings.k8s.json: |-
    {
      "ConnectionStrings": {
        "ActionLogsConnectionString": "{{ .Values.ActionLogsConnectionString }};Application Name=Asp;"
      }
    }

This produce this result:

"ActionLogsConnectionString": "Database=ActionLogs;Server=SQL_DEV;User Id=sa;Password=Y19|yx\dySh53&h;Application Name=Asp;"

Look great! And at this point I don't have a quote problem.

Now problem, the slash isn't escaped for the json file format. Good thing, helm provide a toJson function. Unfortunately, it also transform the "&" to unicode value. I then found toRawJson and it gives the expected results.

My problem is that, when using either toJson or toRawJson, it adds extra quotes and break my result:

so this yalm file:

"ActionLogsConnectionString": "{{ .Values.ActionLogsConnectionString | toRawJson }};Application Name=Asp;"

results in this json file (note the extra quotes):

"ActionLogsConnectionString": ""Database=ActionLogs;Server=SQL_DEV;User Id=sa;Password=Y19|yx\\dySh53&h";Application Name=Asp;"

I see there's a function called | quote, but this only add some. No way to use toRawJson without adding any?

CodePudding user response:

Using toJson or toRawJson is the wrong solution here, because the JSON representation of a string by definition includes the double quotes. "foo" is a JSON string, foo isn't valid JSON.

But you're only working with a scalar value, so there's not much point in marshaling it to JSON in the first place. I think the following gets you what you want:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "asp.fullname" . }}
  labels:
    {{- include "asp.labels" . | nindent 4 }}
data:
  appsettings.k8s.json: |-
    {
      "ConnectionStrings": {
        "ActionLogsConnectionString": {{ printf "%s;Application Name=asp" .Values.ActionLogsConnectionString | quote }}
      }
    }

Here, we're using the printf to produce the desired string (and then passing it to the quote function for proper quoting).

This produces:

---
# Source: example/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-example-fullname
  labels:
    # This is a test
data:
  appsettings.k8s.json: |-
    {
      "ConnectionStrings": {
        "ActionLogsConnectionString": "Database=ActionLogs;Server=SQL_DEV;User Id=sa;Password=Y19|yx\\dySh53&h;Application Name=asp"
      }
    }
  • Related