JSON output for content pages in only 1 section


I’m trying to generate JSON files for content in some specific sections. For example, I have a structure like:

|-- section1
|-- section2
|-- section3
  |-- post1
  |-- post2

and so on. So, I want to generate a JSON file only for all the posts in section 3. How can I do that?

I tried adding section3 = ["HTML", "JSON"] in [outputs] in config.toml, but that didn’t work, or maybe I did something wrong. If this is correct, where exactly should I place the layout for this JSON file? At present, I have it at layouts/section3/single.json.

I think the problem is, the options below [outputs] are “KIND”-based, not section or content type based.

You probably have to enable JSON for posts and then manually remove those in directories you don’t want.

You can create different JSON templates, an “empty default” one and a specific for sections3.
Don’t know, how empty it can be…

I think the “emptiest” must at least contain {} to be proper JSON.

tryed it with XML sitemap, an empty template (zero bytes) will generate nothing, no file there.
Could be the solution - until hugo will dislike it any day.

I thought of this, but I don’t know how to remove the unwanted ones.

Makes sense. So, to sum it up, I need a JSON file in layouts/single.json, which should be empty. And where would the section-specific one go, in the layouts/section/single.json?


Oops, sorry yeah, that’s what I meant. Sadly, that doesn’t seem to work. I can’t see any JSON file generated in my public/section3/post1/<file-name>.json. I’m trying using hugo server --renderToDisk.

Here’s a repo to test: GitHub - Hrishikesh-K/JSON-Test. I need the JSON for the songs section.

I’m probably missing something super simple. Thanks a lot for the help!


shopt -s globstar
rm content/section1/**/*.json
rm content/section2/**/*.json

as a shell script should do the trick. I have a shell script that runs before hugo and one for after hugo for these things. There is a lot hugo can’t and shouldn’t do that I delegate to these release scripts.

Edit: Ehm, I am on Ubuntu. There might be similar solutions for other OSses, but I won’t be dabbling there :wink:

for sections - this are list templates, rename single.json to list.json


section = [“HTML”, “JSON”]
home = [“HTML”, “JSON”, “Logic”, “Sprites”, “Styles”]
section = [“HTML”, “JSON”]
page = [“HTML”, “JSON”]

if you need JSON for every song, create the single.json files

sample list.json

 "songs": [
 {{ range .CurrentSection.Pages }}
	"id": {{.Params.ID}} ,
	"title": "{{.Title}}", 
	"href": "{{.RelPermalink }}"
{{ end }}


 "songs": [
	"id": 1 ,
	"title": "song1", 
	"href": "/songs/song1/"

hope you get one step on …

1 Like

That’s super helpful. Thanks!

Yeah, this seems to work fine! An individual JSON per song would have been better just to keep the JSON file size in control in case the number of songs gets too long. But, I suppose this can work for now.

Thanks a lot again.

EDIT: Seems like it works fine to generate one for individual song too. Great!

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