JSON-LD Bad escape sequence in string. \x26 and \x27

I’m getting the following error: JSON-LD Bad escape sequence in string.

I noticed that becomes \x27 (e.g., doesn\x27t work) and & becomes \x26 (e.g., Windows \x26 Linux).

Example front matter:

---
title: "My Title"
description: "Here's how to fix this problem."
image: "images/image.png"
date: 2017-11-06
---

site-schema.html:

{{- if .IsHome -}}
<script type="application/ld+json">
{
"@context":"https://schema.org",
"@type":"WebSite",
"name":"{{ .Site.Params.name }}",
"url":"{{ .Site.BaseURL }}",
"image":"{{ .Site.Params.image }}"
}}
</script>
{{- end }}
{{- if .IsPage -}}
<script type="application/ld+json">
{
"@context":"https://schema.org",
"@type":"TechArticle",
"name":"{{ .Title }}",
"headline":"{{ .Title }}",
"description":"{{ .Description }}",
"url":"{{ .Permalink }}",
"image":"{{ .Site.BaseURL }}{{ .Params.image }}",
"author":{
"@type":"Person",
"name":"{{ .Site.Params.author }}"},
"publisher":{
"@type":"Organization",
"name":"{{ .Site.Params.name }}",
"logo":{
"@type":"ImageObject",
"url":"{{ .Site.Params.logo }}"}},
"mainEntityOfPage": {
"@type":"WebPage",
"@id":"{{ .Permalink }}"},  
{{ if not .Date.IsZero -}}"datePublished":"{{ .Date.Format "2006-01-02" | safeHTML }}",{{- end }}
{{ with .Lastmod -}}"dateModified":"{{ .Format "2006-01-02" | safeHTML }}"{{- end }}
},}
</script>
{{- end }}

How can I fix this?

There are 3 functions safeCSS, safeHTML and safeJS

Please try to remove ""

- "description": "{{ .Description }}",
+ "description": {{ .Description }},

Thank you. Your solution worked.

1 Like

It works, but why? What to do if we need to concatenate strings?

The template engine will quote the result of {{ .Description }} for you. To concat strings, you should be able to do:

"description": {{ printf "%s%s" .Description .SomethingElse }},
1 Like

see also Hugo 0.55.* breaks structured data rendering which is cleaner imho

2 Likes