Create JSON for list section only


#1

I’ve just found the Output Formats feature which is fantastic! I’m just not sure how to create a .json file for a single section only?

ie I want to create something just for posts/index.json.

I’ve tried creating layouts/posts/list.json but a posts/index.json file doesn’t get created. Do I have to turn on JSON output for the whole posts section? I don’t want JSON files created for this whole section, just the index/list part of them.

Hopefully this makes sense!


#2

There are two items to be enabled to get this working:

  1. A template; you have that covered!
  2. An outputs definition.

There seem to be some confusion with the above, so I’ll take 2) in detail.

You can define outputs at two levels; I will use your concrete use case (the section listing):

Site config:

[outputs]
section = ["HTML", "RSS", "JSON"]

Note that if a page Kind is not in the list above, it will get the “old default” output format, i.e. what Hugo had before 0.20, i.e. HTML and RSS. This also means that if you do put a Kind in there, you will have to supply the full list.

In the example above I say that for the section list I want to output HTML, RSS and JSON. The other kinds of pages, i.e. home page, regular pages etc., will get the “Hugo defaults”.

If you want some special output for 1 page only, this can also be defined (or overridden) in the page frontmatter. In your case, you could create content/posts/_index.md with:

outputs:
- HTML
- RSS
- JSON

In the YAML frontmatter.


#3

Wow that makes so much more sense! Thanks a bunch. Just for anyone else who ends up finding this, the _index.md pages are documented here and custom formats here.


#4

These tools may helps to work with JSON
JSON Formatter
JSON Viewer