Custom Output JSON for specific page

Hi, I’m trying to get Hugo to evaluate a JSON file with Go templates and make it available at localhost:1313/search.json

This is my current directory structure:

│   └───css
│   ├───post
│   └───search
│       └───                         //THIS
│   ├───fonts
│   ├───images
│   │   └───logo
│   ├───js
│   └───rss
        │   ├───css
        │   ├───json
        │   └───sass
        │   ├───partials
        │   ├───search
        │   │   └───list.json                        // AND THIS
        │   └───_default

I have content/search/ with following content:

description: blah
url: /search.json

…and themes/herring-cove/layouts/search/list.json with this content:

  {{ range .Data.Pages }}
      "title"    : "{{ .Title }}",
      "article"  : "{{ .Summary }},
	  "content"  : "{{ .PlainWords }}",
      "tags"     : "{{ with .Params.tags }}{{ delimit . ", " }}{{ end }}",
      "url"      : "{{ .RelPermalink }}",
	{{ end }}

However, I still get a 404 when I try to visit localhost:1313/search.json. Could someone tell me what is missing here?
Any help appreciated! Thanks

We need to see the project or -if not possible- a sample repo that reproduces your problem.

The 404 may be due to several factors, we cannot help from the above description.

1 Like

Let Hugo render the site to publicDir and look what you get






@bep Thanks but, sadly, I still get the 404 at that endpoint.

Here is the repo:

I have removed unnecessary data/files from the site as much as possible and can confirm that this repo still exhibits the same 404 error both at localhost:1313/search.json as well as localhost:1313/search/index.json

If there is any other information that you think would be useful, please let me know!

The Ouput Format definition goes into the project config and not in the front matter of Markdown files. EDIT: It is perfectly possible to define an output per page.

Try the following:

    - HTML
    - JSON
    - HTML

Then move the JSON template to /layouts/_default/list.json

I suggest that instead of placing overrides under the theme directory place them in a /layouts/ folder under the root of your project.

Also never leave the baseURL value blank because parts of the project will not be outputted properly.

If the above does not fix your problems note that I cannot test your sample repo because I do not have PostCSS installed, since I do not use it and I don’t plan to install it either.

Someone else will need to look into your repo if the issue persists.

Also have a look at the Documentation:

1 Like

Thank you for the detailed reply.
I have followed the steps you mentioned but no luck yet.
PostCSS is not needed for this small example so I’ve removed it, if you wish to replicate it locally. The same repo has the latest changes.
Thanks again :slight_smile:

You need to properly format the frontmatter of

description: blah
url: /search.json
- json
1 Like

Thanks! The JSON now shows up as expected! :smiley:

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