Output format URL access

I successfully created a JSON output format with a specific JSON template in layout/_default/single.json. So far so good, I see the corresponding output index.json lying next to my index.html in my dist folder (super neat feature THANKS !!!).

I wish I could access this file at: http://mydomain.com/about.json
instead of (working now) : http://mydomain.com/about/index.json

If I am correct, I should have the .json file named about.json for it to work, right ? How would I do that using Hugo ?

I implemented this feature, and it is super-flexible, but I notice it is hard to explain to people.

In general, the default output formats (JSON etc.) follows the naming convention from the HTML variant.

To get what you want in this case there are a few options:

  1. Define your own JSON output format with a different baseName value (default = index)
  2. Set uglyURLs=true
  3. You can also maybe test setting url in front matter, but not sure how that would behave

Thanks for the prompt reply.

  1. Define your own JSON output format with a different baseName value (default = index)

I’ll try this one, but I think I’ll end up with http://mydomain.com/about/about.json which is not quite what I want. The thing is that my use case may be somewhat complicated after thoughts: to get what I want I may have to deal with the static folder to shortcut the fact that Hugo creates /about/index.html and not /about.html (even if I can make create about.json it will end up within the /about/ sub-directory).

  1. Set uglyURLs=true

Not for me: I need to keep pretty URLs for html files.

  1. You can also maybe test setting url in front matter, but not sure how that would behave

Not sure I understood where to put the url ? If in the frontmatter of my content file, I wonder if this will not only move the .json but also the .html so… I’ll try anyway.

You can redefine the HTML output format and set noUgly=true.

Late reply: got no luck with any of these solutions :confused: No big deal, I’ll stay with normal behavior for now. If that becomes a problem I may just create a post-build script to copy files into static. Thanks for the help anyway :slight_smile:

In a bind, I’d use a redirect. Depending on your webserver, you might even be able to use regex matching for *.json requests, and redirect them to their appropriate, default path.

That first sentence wasn’t a pun, but it would make a good one! :slight_smile:

Ahah
Thanks for the reply.
I use Netlify for Hosting + CI so I don’t have much flexibility…

You’ve got loads of redirect flexibility in that case! :slight_smile:

Yeah I’ve checked this file. But I couldn’t make a general rule to rewrite */index.json queries into *.json or maybe I’ve done something wrong.

Here is what I tried in the _redirects file:

*/index.json   *.json  200

EDIT
after re-reading Netlify’s docs, I think that the rule should be the other way around:

*.json   */index.json   200

But I think this might end-up in some kind of loop…

I think what you are looking for is:

/*.json   /*/index.json   200

You can check it in the Netlify redirects playground.

Sorry, I don’t use Netlify, I just really like their redirect system. I’d test it for ya, but I don’t have an account. :slight_smile:

Nice try :slight_smile:
But got no luck either trying your solution…

However I confirm this works:

/about.json   /about/index.json   200

So I could stuff all my redirects here one by one somehow :confused:

I am sure you could arrange to have that file auto-filled by ranging over content that produces the custom output, but… that isn’t really the point here. This was a shortcut if you needed it for only a few redirects.

I haven’t actually tried to fix your issue in a “proper” way, and doubt I’ll be able to. But someone will figure it out, or create an issue asking for a fix. :slight_smile:

Yeah no prob, that was a mere visual necessity: I can live with it for now.

I’d rather find a more idiomatic way than using my hosting tricks as well.
The easy move would be to have a post-build script looking for all *.json files and move them at the root of the dist folder. I am sure Go would do that in a breeze, but after looking for a proper pre/post build workflow here I found most devs end up using Gulp + Js to handle such things. So I might simply use node.js built-in fs package to move files around.

I think my web server is configured to disallow AJAX requests for .json files even if it’s on the same domain. I got around this by creating a custom output type with a .js extension instead. I know this is kinda hacky, but I do not have control over my web server to make any administrative changes.

You can use a content file with a url parameter to indicate where you would like your about.json file to be output.

Hope this helps!

Ahah Good one !!
Did not tested but I will. :slight_smile:
Thanks