JSONIFY is escaping strings

The hugo function jsonify is escaping characters.


{{ $testDict := dict “<any-key>” “value”}}
{{ $testDict | jsonify }}



Is this a bug with jsonify or is there a hugo function I’m missing?

Any help would be appreciated, thanks!

1 Like

Have a look at safeJS or the other safeX functions.

I have the same issue as the original poster here, but I’m not sure how to apply the suggested solution of using safeJS.

{{ $value := "<b>a thing</b>"}}
{{ $content := dict "key" $value }}

{{ $content.key }}
{{/* <b>a thing</b> */}}

{{ $content.key | jsonify }}
{{/* “\u003cb\u003ea thing\u003c/b\u003e” */}}

{{ $content.key | safeJS | jsonify  }}
{{/* “\u003cb\u003ea thing\u003c/b\u003e” */}}

Thanks in advance for any help!

I’m running into this right now as well. I wonder if it’s a combination and order of these functions that will do the trick. I’ll report back if I can figure it out.

The thing to note here is, that this is about a KEY, not the values of JSON.

Could you provide a link to the JSON specification where special characters like that are allowed in KEYs? I never saw a key using other characters than a-z0-9-_. Maybe there is a rule that no HTML is allowed in keys.

Also, look at the deeper issue here, why would anyone require HTML for a key? If that is something that is used for design, it should be handled in the layouts. If it’s to mark one specific collection of items (it’s a new item, make it red) then the structure should have a "new": true in there and THAT should add the design.

jsonify is doing what is right in my opinion. A key has a function: to tell us what the value is appointed to. And HTML has no right to live there IMHO :wink: safeJSON probably works in values with HTML.

This is a good point. I agree with your sentiment.

I didn’t read this well. I’m having the same problem as the OP, but with the value not the key.

I remember reading a post recently where the solution was to use safeJSON on the special character values, but NOT add extra " around the value and then jsonify the whole thing. Not sure if that fits this issue.

Sorry to be slow responding, and thanks for your answer!

But in my example "<b>a thing</b>" is the value. The key is "key":

{{ $value := "<b>a thing</b>"}}
{{ $content := dict "key" $value }}

{{ jsonify $content }}


{“key”:"\u003cb\u003ea thing\u003c/b\u003e"}

It’s not relevant since this is not a key, but from https://tools.ietf.org/html/rfc8259:

  1. Objects

An object structure is represented as a pair of curly brackets
surrounding zero or more name/value pairs (or members). A name is a
string. A single colon comes after each name, separating the name
from the value.

  1. Strings

The representation of strings is similar to conventions used in the C
family of programming languages. A string begins and ends with
quotation marks. All Unicode characters may be placed within the
quotation marks, except for the characters that MUST be escaped:
quotation mark, reverse solidus, and the control characters (U+0000
through U+001F).

But I do agree that including markup in keys would be pretty weird.

I’m afraid I don’t really understand this. Do you mean use safeJS (not safeJSON?) on "<b>a thing</b>", or just on the < and > characters somehow? And I’m not sure where I am adding the extra " around the value.