In my template, I try to read in the json file using:
{{ $dataResource := (.Resources.GetMatch “data.json”).RelPermalink }}
{{ $data := getJSON $dataResource }}
When generating, the following errors occur:
Cannot read json from resource /de/trekkings/tourbregaglia/data.json with error message unexpected end of JSON input
Cannot read json from resource /nl/trekkings/tourbregaglia/data.json with error message unexpected end of JSON input
Cannot read json from resource /trekkings/tourbregaglia/data.json with error message unexpected end of JSON input
I tried it with different valid json files (valid according to jsonlint.com), even the most simple ones, the error always occurs.
Load Local files
To load local files with getJSON and getCSV, the source files must reside within Hugo’s working directory. The file extension does not matter, but the content does.
Did you check if the paths coincide .RelPermalink and “Hugo’s working directory”?
Thanks for the hint.
If I change my template now to
{{ $dataResource := (.Resources.GetMatch “data.json”).RelPermalink }}
{{ $data := getJSON “content” $dataResource }}
it works for the default language (english), but not for the other two languages, because the RelPermalink includes ‘de’ or ‘nl’, and there is no /content/nl/ or /content/de/ directory…
So my question ‘getJSON returns Cannot read json’ is solved (thanks Mikhail!) but another question arises then: how to use Data Driven Content (as described here https://gohugo.io/templates/data-templates/#data-driven-content) for a multilingual site? Will post a new post for that question
I did not make multilingual sites. I can advise looking at the variables.Lang and .Language. And use the language prefix in the file name. /data/en.json etc.