Possible to make a read-only JSON API of the content folder or other JSON from a config file?

title says all

I did find this (https://forestry.io/blog/build-a-json-api-with-hugo/) but the problem is that I don’t want to stop outputting HTML files and just add JSON alongside HTML ones.

Not even thinking about what the head sends.

Would be especially awesome to have like a index.json or something like it where I could have the .Site.Title displayed, some config options, last update, etc.

For reference: https://github.com/cstate/cstate/issues/27

You can use multiple output formats in parallel. The tutorial you reference just uses json as an example. This should work.

[outputs]
    page = ["json", "html"] # A player
    section = ["json", "html"] # All players
    home = ["json", "html"] # Everything

Okay, this… doesn’t work.

If I set json to be the first thing, then no HTML is generated.

If I set html to be the second thing, then no JSON is generated.

This is more nuanced than that it seems?

Actually, there’s no template, maybe that’s why.

WARN 2019/05/09 20:10:51 Found no layout for "home", language "en", output format "JSON": create a template below /layouts with one of these filenames: index.en.json.json, home.en.json.json, list.en.json.json, index.json.json, home.json.json, list.json.json, index.en.json, home.en.json, list.en.json, index.json, home.json, list.json, _default/index.en.json.json, _default/home.en.json.json, _default/list.en.json.json, _default/index.json.json, _default/home.json.json, _default/list.json.json, _default/index.en.json, _default/home.en.json, _default/list.en.json, _default/index.json, _default/home.json, _default/list.json

Please have a look at

Also please do not post super long questions in the topic titles because it litters the forum. I condensed the title.

Thank you

Sorry about that! Won’t do it again. Last I looked it wasn’t helpful or I just didn’t understand it

I’ve seen folks get decent results when they hit the docs and then ask questions and clarifications about the examples there. We like keeping the docs useful, so linking to the part you don’t understand really helps us all. :slight_smile:

I wrote a post a while back about adding json outputs, a search index and a json feed, to Hugo.

There is one built in json output format in Hugo but you can easily add as many as you need. Here is one example adding a “SearchIndex” json output.

outputFormats:
  SearchIndex:
    mediaType: "application/json"
    baseName: "searchindex"
    isPlainText: true
    notAlternative: true

outputs:
  home: ["HTML","RSS", "SearchIndex"]

Then you just add a template file for it with whatever output you need. In my post below you find two examples. If possible I recommend building up an array (slice) and run it through “jsonify”. That way you are sure you output valid json.

https://xdeb.org/post/2017/06/11/make-hugo-generate-a-json-search-index-and-json-feed/

1 Like