Output formats - any help yet?

Back when output formats (http://gohugo.io/extras/output-formats/) I tried my best to grok the docs but am still failing to get how this is used in the real world. I’m starting with what I assume is a simple demo. I want to build a page to output JSON.

I first tried this:



Which I recognize isn’t valid JSON (the stuff at the bottom), but I assumed that hitting /feed/json would return the content by itself with no layout. However, it still uses the main site layout. So am I just using this feature wrong?

As a follow up, obviously I don’t want hard coded JSON, but something dynamic. Hugo still does not want you to write code in your /content folder, correct? So I’d need to build a shortcode perhaps and put the logic there?

Have a look at this theme’s example site. It defines the outputs on a site level (i.e. in config.toml), but it is also do specify this on page level i.e. in page frontmatter.

If you look at the above, it specifices that it wants the home page rendered as HTML, RSS and JSON, and then provides templates for the JSON homepage:

So given a new page, let’s say foo.md. If I specify JSON as an output, and only json, should it expect to find /layouts/foo.json to handle rendering it?

So I tried that since - obviously- it takes about 2 seconds. :wink: Given a file under content with the name jsonfeed.md, and this source:



I then added layouts/jsonfeed.json, and as far as I can tell, it’s not rendering right.

No, but again, this is well documented and I have given you a concrete example. Re. the templates, the behaviour mimics that of the HTML output format. And you would never have expected Hugo to have a 1:1 mapping between the Markdown pages and the templates?

I apologize as I’m simply having difficultly mapping the example you linked to to the more general concept of how this is supposed to work. So given my page, let’s say /content/foo.md, where my intent is for it to output JSON, it sounds like you are saying I’m perhaps missing the layout front matter to tell it what layout to use. Is that right? But I had thought that this new feature supported the layout automatically.

I think my issue is, given /content/X,md, how do I get it to run a layout that will be just JSON and nothing more. Using the outputs front matter is required, but I don’t know the next step.

You need a template. You can either be specific (by setting the layout=somelayout in frontmatter), but just to get something working I would suggest just adding:


Hugo’s template handling is a core feature to understand, even if your output format is just plain old HTML.

I think my confusion was in thinking it was more automatic - if that makes sense. I’ll dig and report back.

Ok, so I’ve made some progress. I created /layouts/_default/single.json. With my content page saying it outputs just JSON, my assumption is that it would load this template automatically. Now when I run my request, I don’t get the regular HTML output, I get absolutely nothing. So I think I’m on the right track, but can you explain why nothing is being shown?

My template is just this for now:

{{ .Content }}

which I know isn’t valid JSON output, but for now I just want to see my output - I’ll make it real later.

What do you mean shown? It may be good to just run hugo and see what files gets built into /public.

And like magic - I finally got it. Running the output helped flesh out where the file was being created. I think one of the biggest issues I have with Hugo is that I think of layouts as always being wrappers, not actually generating page content as well. Thank you for being patient. My goal is to generate a new template for this: https://jsonfeed.org/

When I get this working, I’ll blog, and post back here. I’d love your opinion on my solution (in case I do something stupid :wink:

1 Like

I have not been able to get it to work. I have a simple search index I am trying to create with json. My file is at content/json/search.json (same if I do search.md or put it at /content/search.json (or search.md), it just doesn’t appear to run it through the template.

The search.json is simple:

outputs: ["json"]
layout: "json"
url: /search.json

And yet when I try to view the page, I see just the raw content, i.e. the front matter.

I tried putting the layout in layouts/json/single.json, in layouts/_default/single.json (and tried for list.json as well).

Also interestingly, it completely ignores url: /search.json , placing it under whatever directory I have it at the moment.

@Raymond_Camden, how did you get it? Which files did you place where?

One step up, it seems to generate it as json/index.html and json/index.xml, rather than /search.json.


  1. It isn’t running it through json/single.json but rather _default/single.html
  2. It isn’t respecting the url: to make it a single json page

OK, ignore, idiot question. I was using layout: instead of type:. Doh!

Sorry for the delay - I was on vacation. I wrote up my solution here:

I still feel like this support needs doc help as I think I had to pretty much guess my way to a solution.


Please search the forums for examples, there are plenty. Creating JSON from .md files is a frequent use case, as it assist in creating search indices.