Populating Templates with JSON?

Hello I’m looking at Hugo as our go-to static site generator and I’m wondering if there was a way to populate templates with information from JSON files? I am working with an API that sends me posts and page information as JSON and I want to use Hugo to build files from that information and then build/deploy site afterwards.

Is this possible using Hugo?

Yes. Two ways, one even remotely. Look at the docs for more.

Well, yes and no.

Hugo can ingest JSON and use that data on existing pages (see here and here); however, Hugo doesn’t currently support creating new pages from a JSON data source. Generally speaking, Hugo requires content files in the content folder for each page.

1 Like

Ah so I just need to write a script to populate a file with whatever I want and have Hugo build after that. Thanks for the links to the docs!

@newton Did you write that script? I’m currently using jekyll with a node script that pulls data down from a REST API and then using a cron to build the site over and over. Something like this would be ideal and I’d switch.

Unfortunately no, we decided to go with a custom JS script that uses Handlebars for templating (Handlebars is a mess :frowning: ).

I’m looking into that too. I just need to figure out some way to build somewhat simple sites based on REST data that isn’t “magic.” I really don’t want to get into big frameworks like Angular if I don’t have to.

We ended up using Gulp but npm scripts could have been better. Also anytime a entry is published our CMS triggers webhook that publishes the content, which eliminates the need for cron jobs.

Sounds like we are doing something really similar. Unfortunately, the CMS I use doesn’t offer a webhook (for now). So I’m caught in this cycle.

Ah what a bummer, I would submit a support ticket to your CMS.

Nothing I have read in the docs or at any of these links makes it clear how I simply dump some JSON on a page via a template or partial. Any pointers would be appreciated, thanks.

1 Like