I was reading this post https://discourse.gohugo.io/t/unmarshall-error/53124/1 and came across a similar issue that’s been hard to resolve

I was reading this post gohugo.io/t/unmarshall-error/53124 and came across a similar issue that’s been hard to resolve.

I’m trying to unmarshal a JSON string directly in a Hugo template, but I keep getting errors related to parsing, particularly when using transform.Unmarshal. The JSON structure looks valid, but it seems Hugo misinterprets it as YAML or CSV due to auto-detection issues. Here’s an example of the data:

[
  "abc123",
  "def456",
  "ghi789"
]

Even when wrapping it as a string and attempting to process it with resources.FromString or explicitly naming it as a JSON resource, I encounter inconsistent behavior. At times, the template processes it correctly, but other times, it throws errors like “bare ’ in non-quoted field” or fails silently.

What makes this more complicated is that the data source varies, and I cannot hard-code the resource name every time. I need a more dynamic and robust solution for handling JSON inputs, especially when Hugo’s auto-detection isn’t reliable.

Has anyone found a way to force Hugo to recognize JSON consistently without running into auto-detection conflicts? Any help or workarounds would be appreciated!

you could wrap your array into a JSON map, unmarshal and extract the array.

{{ $string := `["abc123",  "def456", "ghi789" ]` }}
{{ $wrapped := add `{ "wrapped": `  $string  "}" }}
{{ $json := transform.Unmarshal $wrapped }}
{{ $data := $json.wrapped }}
{{ highlight ($data | jsonify (dict "indent" "  ")) "JSON" }}

todo: add error checking

or for the resource.FromString generate a SHA hash or other based on input as filename