getJSON returns Cannot read json

Hi,

For a multilingual site, I would like to include a json data file as page resource.
My content folder looks like this:

trekkings
| tourbregaglia
| | index.nl.md
| | index.de.md
| | index.en.md
| | data.json

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.

Any help would be appreciated!

David

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”?

Hi Mikhail,

I don’t understand what you mean by that. Could you be more precise what I should check?

The RelPermalink in my case equals ‘trekkings/tourbregaglia/data.json’, Hugo’s working directory is the root directory of my site I guess?

David

The RelPermalink trekkings/tourbregaglia/data.json

Files path /content/trekkings/tourbregaglia/data.json

It does not look the same.

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 :slight_smile:

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.