Problems with encoding special characters in json+ld ("invalid escape sequence")

I am having some problems with escape sequences in my json+ld data. Example:

My json+ld is:

<script type="application/ld+json">
{
  "@context" : "http://schema.org",
  "@type" : "Webpage",
  "name": "{{ .Title }}"
}
</script>

.Title contains the value “My & Company”. What it becomes is:

<script type="application/ld+json">
{
  "@context" : "http://schema.org",
  "@type" : "Webpage",
  "name": "My \x26 Company"
}
</script>

Google validator says: There was an error parsing your JSON-LD. Bad escape sequence in string. See: https://developers.google.com/structured-data/testing-tool/

Any ideas? I think the template is just escaping it wrong. It should use HTML entities.

This seems odd, but a fix in your case would be to:

"name": "{{ .Title | safeJSS }}"

But when I test something similar, I get

<script type="application/ld+json">
{
  "@context" : "http://schema.org",
  "@type" : "Webpage",
  "name": "My \x26 Company"
}
</script>

Which is valid …

What OS (Windows?) and Hugo version are you using?

Mac OS X, v0.15

I mean, the problem is the “\x26”. Maybe Google’s validator is wrong if that is actually a legal character. safeJS doesn’t change the output for me at all btw.

I know … I saw that when I posted it I got the same … that is this forum with copy and paste code. I’m not seeing the x26, but 0026 which is valid.

Where are you seeing this? The forum did not change anything for me. What I entered is what I got.

Try this:

"name": {{ .Title | safeJS }}

I’m seeing something similar.

My description in the post front matter is as follows.

description: It's driving her wild!

My header.html template has this.

{{ if .IsPage }}
<script type="application/ld+json">
{ 
    "@context": "http://schema.org",
    "@type": "Article",
    "headline": "{{ .Title }}",
    "author": {
        "@type": "Person",
        "name": "http://profiles.google.com/{{ .Site.Params.gplus }}?rel=author"
    },
    "datePublished": "{{ .Date.Format "2006-01-02" }}",
    "description": "{{ .Description | safeJSS }}",
    "wordCount": {{ .WordCount }}
}
</script>
{{ end }}

And this is what is getting rendered. (notice the ` has been replaced by \\x27

"description": "It\\x27s driving her wild!"

Im on Windows 8 and Hugo 0.15.

I tried the fix suggested above i.e.

"description": "{{ .Description | safeJSS }}",

But that causes the following error.

ERROR: 2016/04/22 template: theme/partials/header.html:48: function “safeJSS” not defined
INFO: 2016/04/22 found taxonomies: map[string]string{“tag”:“tags”, “category”:“categories”}
ERROR: 2016/04/22 html/template: “theme/partials/header.html” is an incomplete template in theme/partials/header.html

Edited my previous post to correct a typo. It’s safeJS, not safeJSS. And take note of the quotation marks in my previous post. They’re important.

1 Like

It’s not wotking for me. Both safeJS (for the description) and safeURL (for the url) are not working.

My partial:

"description" : "{{ .Description | safeJS }}",
"url" : "{{ .Permalink | safeURL }}",

What I get

"description" : "There\x27s still a couple of interesting shows to premiere ",
"url" : "http:\/\/localhost:1313\/posts\/soon-on-tv-aug2016",

I have no idea what I’m doing wrong. I’m using Hugo v0.16 on windows.

SOLUTION:

Seems that the url is not a problem, the json-ld plyaground validates it as correct. For the description:

"description" : {{ printf "\"%s\"" .Description | safeJS }},

2 Likes

My previous suggestion should work. You shouldn’t need the printf. Pay close attention to the double-quotes.

"description": {{ .Description | safeJS }},

Your suggestion gives me this:

"description" : September is here, and there's a few interesting shows coming. Let's check them below, in order of interest!,

The text is ok, but of course, the google validador complains of the absence of quotes.

1 Like

@thiagomgd What if you wrap your double curlies in quotes? I’m using Hugo for JSON LD without issue.

If I do this:

"description" : "{{ .Description | safeJS }}",

I got \x27 instead of '

thank you for save my time!

I have exactly the same issue that @thiagomgd had: I use the current windows release (v0.55.6-A5D4C82D windows/amd64).

If I use:

"description": {{ range where .Site.RegularPages "Section" .Params.inits_folder}}{{ .Content | safeJS }}{{ end  }},

I get plain HTML, but without leading and ending quotes, which is not pure JSON.

And if I use quotes:

"description": "{{ range where .Site.RegularPages "Section" .Params.inits_folder}}{{ .Content | safeJS }}{{ end  }}",

the result is strangely escaped (\x27 instead of ').

The solutions with printf seem also not to work:

printf "\"" # prints exactly "\""
print "     # results in error

This worked great for me - I’m using https://search.google.com/structured-data/testing-tool