Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No template function for JSON string escaping #5

Open
adeverteuil opened this issue Oct 14, 2022 · 0 comments
Open

No template function for JSON string escaping #5

adeverteuil opened this issue Oct 14, 2022 · 0 comments

Comments

@adeverteuil
Copy link

I tried using the printf "%q" function in go to encode double-quoted strings (like this: {{ printf "%q" .Annotations.description }}). This renders correct JSON strings in most cases, but it's not perfect.

I wasn't able to figure out how to use the jq template function. alertmanager-webhook-proxy crashes when I try to load the template.
I tried {{ jq .Labels.example }} and {{ .Labels.example | jq }}.

Reference documentation:

Similarities between Go string literals and JSON strings:

  • Double quotes delimiters.
  • Two character escape sequences \\, \", \n, \t, and others.

Differences:

  • Go literal strings have \nnn (octal), \xFF, \uFFFF and \UFFFFFFFF character sequences.
  • JSON strings only have \uFFFF and a few 2-character sequences.

For example, in Go printf "%q" a null character will be represented as \x00, but JSON would require \u0000.

In JSON, all control characters U+0000 to U+001F must be escaped. Therefore, any control character that would not be represented as a 2-character escape sequence would produce bad JSON and cause an error.

Resolution:

I ended up re-writing in Python (because I am more familiar with that) so that I can use a JSON serialiser instead of a text template engine. This way, at least the JSON syntax is always correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant