Generate a page from a JSON/TOML file

Good evening everyone,

I’m using v. 0.62.0, and I’m trying to understand how to generate a page from a data file (JSON, TOML, whatever). I don’t want to generate multiple pages from a single data file (as asked here); I just want to be able to print the data inside that single page.

As I understand, it should be possible because in the Page Bundles page it says the index file name’s extension of a Leaf Bundle can be json or any other valid MIME type. If I create an containing just the front matter, the html page is generated. If I, however, rename the as index.json, Hugo does not generate the page, nor output any error.

An acceptable workaround would be having an file and a separate data file that gets loaded by the page template. However, I didn’t find a way to load a data file that’s not stored inside the data folder. I’d prefer to keep data files inside the Leaf Bundle folder.

Am I missing something?

Using this approach, you would get the JSON file as a page resource in your template, then work with it from there.

Thank you. Everything is quite new to me, and I couldn’t figure out how to load a local JSON.

In case someone else will have the same problem, this worked perfectly: {{ $list := getJSON "content" (.Resources.GetMatch "list.json").RelPermalink }}.

1 Like