Fixing the Google Search Console error 'Bad escape sequence in string' when using hugo and JSON-LD


If you use JSON Linked Data (JSON-LD) to add Google-friendly structured data to your website, you might have come across the following error in your Google Search Console:

Unparsable structured data
Structured data with syntax errors detected
Bad escape sequence in string

Looking at the search console’s help page for that error reveals the following information:

Error typeDescription
Bad escape sequence in stringAn invalid escape sequence used in a string value. For example: “description” : “Some \q unknown sequence”

In my specific case, the single quote character ' in the original text was being transformed into this mess in the JSON-LD description field: \x26rsquo. This is mixture of unicode and HTML entity. :) \x26 is unicode for the &-character. And &rsqou is the HTML entity for the original (right) single quote '-character.

Ok, but how do I fix the error?

If you are using the hugo static site generator, like me, you first need to find the template that generates your structured data. If you don’t know where to look, try searching all your template files for "@context" : "http://schema.org".

In one of those files you will find a line looking something like this (I have unwrapped the single for readability):

1
2
3
4
5
6
7
8
"description" : 
        {{ if .Description }}
        {{ .Description | plainify }}
        {{ else }}
            {{if .IsPage}}
                {{ .Summary | plainify  }}
            {{ end }}
        {{ end }}",

Look at line 3 and 6, using the | plainify. This pipes the string into hugo’s plainify-function that, according to the documentation “Strips any HTML and returns the plain text version of the provided string.” For our purpose, that is not enough. Turns out, that this is not enough when you are encoding for JSON-LD. I have found that adding htmlUnescape before using plainify will make sure that none of this strange double-encoding happens. The end result looks like this (line 3 and 6):

1
2
3
4
5
6
7
8
"description" :
    {{ if .Description }}
        {{ .Description | htmlUnescape | plainify }}
    {{ else }}
        {{if .IsPage}}
            {{ .Summary | htmlUnescape | plainify  }}
        {{ end }}
    {{ end }}",

If you want to test if this fixes your problem, Google provides a handy test-tool for that: Rich Results Test.


See also