Formatting URL's in JSON string

I am trying to add URL to JSON schema which looks like

"mainEntityOfPage": {
      "@type": "WebPage",
      "@id": "{{ .Site.BaseURL }}"
}

This renders out like this

"mainEntityOfPage": {
      "@type": "WebPage",
      "@id": "http:\/\/domain.com\/"
}

How should I make http:\/\/domain.com\/ to http://domain.com/? I tried using safeHTML and absURL but I still get the same output.

I am having trouble visualizing what you are trying, and understanding the context. Piping to safeHTML seems like it should work. When I have generated json in the past, say for a search index, I have used jsonify. You can pipe to multiple functions as well:

"@id": "{{ .Site.BaseURL | htmlUnescape | safeHTML }}"

I had a feeling I had something in the form of "url": "{{.Site.BaseURL}}" somewhere in my sites, so I went digging, and it looks like behaviour may have possibly changed between 0.54 and 0.55?.

So if I have the following inside my head:

<script type="application/ld+json">
  {
  "url54": "{{.Site.BaseURL }}",
  "url55": {{.Site.BaseURL | jsonify | safeJS }}
  }
</script>

I get the following output:

<!-- 0.54 -->
<script type="application/ld+json">
  {
  "url54": "http://localhost:1313/",
  "url55": &#34;http://localhost:1313/&#34;
  }
</script>
<!-- 0.55+ -->
<script type="application/ld+json">
  {
  "url54": "http:\/\/localhost:1313\/",
  "url55": "http://localhost:1313/"
  }
</script>

TLDR: you may have to pipe .Site.BaseURL through jsonify and safeJS in 0.55+

Interestingly, .Permalink and url strings seem to be unaffected.

1 Like

Adding {{.Site.BaseURL | jsonify | safeJS }} is giving me \x22http:\/\/localhost:1313\/\x22 and adding {{ .Site.BaseURL | htmlUnescape | safeHTML }} is giving me http:\/\/localhost:1313\/.

what version of Hugo are you using?

I am on 0.55.3

It works for me the way I described above, on 0.55.3. Could you try the example input exactly? If you are getting different results, do you have a repo or a minimal example we can look at to test?

Yes. Repo is at: https://github.com/akshaybabloo/gollahalli.com For example, I have this line:

I have used this only in AMP for now, so if you run hugo server and go to http://localhost:1313/amp/blog/azsecrets-a-cli-to-set-azure-key-vault-as-environment-variables/ then view the source.

Try it without the quotes around the curly braces:

"@id": {{ .Site.BaseURL | jsonify | safeJS }}
1 Like

OK that worked, I am going to report this breakage.

@akshaybabloo The same situation. I changed from 0.54 to 0.55 and there were backslash in url inside the scripts. It took me hours to find this thread.
Very confusing to use it without " "
Anyway "url": {{ .Site.BaseURL }}, works. But I still think it is a bug

If I need something like this:
"url": "{{ .Site.BaseURL }}/hello-world"

I have to use this syntax:
"url": {{ add .SiteBaseURL "hello-world" }} Strange…

if hello-world.md is a content file

{{ with site.GetPage "hello-world.md" }}
   "url": {{  .Permalink }}
{end}}
1 Like