getJSON: Unexpected end of JSON input

I cannot get accessing JSON either under the data directory or loading local JSON with the getJSON function to work.

Here is the attempt to access JSON from a data template:

    `{{/* JSON */}}
    {{ $json := index $.Site.Data.jaysawn "firstkey" }}
    <h2>{{ $json }}</h2>

And here is the YAML version:

    {{/* YAML */}}
    {{ $yaml := index $.Site.Data.yammel "firstkey" }}
    <h2>{{ $yaml }}</h2>`

Here is the JSON file data/yammel.json:

    {
    "firstkey": "firstvalue"
    }

And here is the YAML file data/yammel.yaml:
"firstkey": "firstvalue"

When trying to access JSON from a data template, the index call fails with:
"error calling index: index of untyped nil"
Note that accessing the YAML works just fine.

When trying to access the JSON as a local file via getJSON, I get:
unexpected end of JSON input
I’ve tried putting the json file at the top level of the site directory, under the data dir, and other places, but always the same error.

Am I doing something wrong? Or is JSON just not working? I have a lot of data I need to access one way or another in templates, and the data is natively JSON. I could convert it to YAML with yq or similar, but it would add another step to an already lengthy process, so I’d rather not if I can get Hugo to read the JSON. Any thoughts or suggestions greatly appreciated!

From the errors you posted the JSON does not seem valid.

Have you tried validating it? See: https://jsonlint.com/

Also JSON support is fine in Hugo v0.80.0 as well as in many previous versions, as far back as I can remember.

Furthermore see the forum Requesting Help guidelines.

So for posterity, this was the problem: I had .json$ in the ignoreFiles array in the config file. I had thought that ignoreFiles was for keeping certain types of files out of the final build, but apparently it also disables JSON files in the data directory? Is this the intended behavior? If so, is there some other way of making sure that JSON files don’t make it into the final build?

In any case, removing JSON from ignoreFiles solves the problem or not being able to read JSON in data templates.

In the ignoreFiles array, you can specify the folder in which you want to disable JSON or other file formats, e.g. "/content/\\.json$"

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.