Is there an easy way to render raw text read by readFile as "pretty printed" JSON?

I’m stuck on what I assume is a newbie problem. I want to insert JSON-LD into my header, and the json is in files in my ./data/section/whatever.json files. What I’ve done is this:

  <script type="application/ld+json">
{{ readFile (printf "./data/%s/%s.json" .Section (path.Base .RelPermalink)) }}
  </script>

Snag is, what’s output is this:

  <script type="application/ld+json">
"{\n\t\"@context\": \"https://schema.org\",\n\t\"@type\": \"BarOrPub\",\n\t\"address\": {\n\t\t\"@type\": \"PostalAddress\",\n\t\t\"addressCountry\": \"ZA\",\n\t\t\"addressLocality\": \"Parkview\",\n\t\t\"addressRegion\": \"Johannesburg\",\n\t\t\"postalCode\": \"2132\",\n\t\t\"streetAddress\": \"Princess of Wales Terrace\"\n\t},\n\t\"geo\": {\n\t\t\"@type\": \"GeoCoordinates\",\n\t\t\"latitude\": \"-26.1622097\",\n\t\t\"longitude\": \"28.0304166\"\n\t},\n\t\"hasMap\": {\n\t\t\"@type\": \"Map\",\n\t\t\"url\": \"https://www.google.co.za/maps/place/Zoo+Lake+Bowls+Club/@-26.1622097,28.0304166,17z/data=!3m1!4b1!4m5!3m4!1s0x1e950c637be96a97:0x647573ab4607e2cc!8m2!3d-26.1622076!4d28.0330357\"\n\t},\n\t\"name\": \"Zoo Lake Bowls Club\",\n\t\"telephone\": \"0114860843\",\n\t\"url\": \"http://www.zoolakebowls.co.za/\"\n}\n"
  </script>

Before diving into trying to fix this with replaceRE (I hardly ever use regular expressions, and my heart sinks every time I have to), I wondered if there’s some built in trick?

I’ve tried this

  <script type="application/ld+json">
{{ jsonify (index .Site.Data .Page.Section) }}
  </script>

But again it produces raw text instead of JSON.

Sorry, thought I had something, doesn’t work.

I’ve managed to get what I want using jsonify if I put it in the body, but not in a script tag in the head for some reason.

To illustrate, if I put this in ./layouts/_default/single.html

{{ $json_map := (index (index .Site.Data .Page.Section) (path.Base .RelPermalink)) }}
<pre>
{{ jsonify $json_map }}
</pre>

gives me what I want:

<pre>
{"@context":"https://schema.org","@type":"BarOrPub","address":{"@type":"PostalAddress","addressCountry":"ZA","addressLocality":"Parkview","addressRegion":"Johannesburg","postalCode":"2132","streetAddress":"Princess of Wales Terrace"},"geo":{"@type":"GeoCoordinates","latitude":"-26.1622097","longitude":"28.0304166"},"hasMap":{"@type":"Map","url":"https://www.google.co.za/maps/place/Zoo+Lake+Bowls+Club/@-26.1622097,28.0304166,17z/data=!3m1!4b1!4m5!3m4!1s0x1e950c637be96a97:0x647573ab4607e2cc!8m2!3d-26.1622076!4d28.0330357"},"name":"Zoo Lake Bowls Club","telephone":"0114860843","url":"http://www.zoolakebowls.co.za/"}
</pre>

But doing pretty much exactly the same in /layouts/partials/head.html

{{ $json_map := (index (index .Site.Data .Page.Section) (path.Base .RelPermalink)) }}
  <script type="application/ld+json">
{{ jsonify $json_map }}
  </script>

gives me the escaped raw text blues

<script type="application/ld+json">
"{\"@context\":\"https://schema.org\",\"@type\":\"BarOrPub\",\"address\":{\"@type\":\"PostalAddress\",\"addressCountry\":\"ZA\",\"addressLocality\":\"Parkview\",\"addressRegion\":\"Johannesburg\",\"postalCode\":\"2132\",\"streetAddress\":\"Princess of Wales Terrace\"},\"geo\":{\"@type\":\"GeoCoordinates\",\"latitude\":\"-26.1622097\",\"longitude\":\"28.0304166\"},\"hasMap\":{\"@type\":\"Map\",\"url\":\"https://www.google.co.za/maps/place/Zoo+Lake+Bowls+Club/@-26.1622097,28.0304166,17z/data=!3m1!4b1!4m5!3m4!1s0x1e950c637be96a97:0x647573ab4607e2cc!8m2!3d-26.1622076!4d28.0330357\"},\"name\":\"Zoo Lake Bowls Club\",\"telephone\":\"0114860843\",\"url\":\"http://www.zoolakebowls.co.za/\"}"
  </script>

I initially thought this may have to do with using a partial, but moving the head.html partial code into baseof.html made no difference. So it seems related to how Hugo puts content into the element.

Pipe it through the safeJS function.

Perfect! Many thanks.

Here is my final way of doing it (reading from file to preserve original JSON-LD formating)

<script type="application/ld+json">
{{ safeJS (os.ReadFile (printf "./data/%s/%s.json" .Section (path.Base .RelPermalink))) }}
</script>