HUGO

JSON output uses the wrong template

I have two sections, Projects and Universities, where I want Hugo to generate JSON and HTML outputs.

The JSON for Universities works perfectly, but the JSON file for Projects is using the template for University. Is there a bug in Hugo’s lookup order or am I missing something?

Project Structure

├── config.toml
├── content
│   ├── about
│   ├── contact
│   ├── _index.md
│   ├── news
│   ├── projects
│   │   ├── project-1.md
│   │   └── project-2.md
│   ├── resources
│   └── universities
│       ├── a.md
│       ├── b.md
│       └── c.md
├── data
├── layouts
│   ├── _default
│   │   ├── baseof.html
│   │   ├── li.html
│   │   ├── list.html
│   │   ├── single.html
│   ├── index.html
│   ├── index.json
│   ├── news
│   │   └── single.html
│   ├── partials
│   ├── projects
│   │   ├── li.html
│   │   ├── list.html
│   │   ├── list.json
│   │   └── single.html
│   ├── resources
│   │   ├── li.html
│   │   └── single.html
│   ├── section
│   │   ├── about.html
│   │   └── contact.html
│   └── universities
│       └── list.json
├── resources
└── static

universities/list.json

{{ define "main" }}
[
{{ range $index, $e := .Data.Pages }}
    {{ if $index }}, {{ end }}
    {
        "name": "{{ .Title }}",
        "location" : "{{ .Params.Location }}",
        "website" : "{{ .Params.Website }}",
        "logo" : "{{ .Params.Logo }}"
    }
{{ end }}
]
{{end}}

projects/list.json

{{ define "main" }}
[
{{ range $index, $e := .Data.Pages }}
    {{ if $index }}, {{ end }}
    {
        "name": "{{ .Title }}",
        "permalink": "{{ .Permalink }}",
        "university" : "{{ .Params.university }}",
        "date" : "{{ .Date.Format "02 Jan 06 15:04 -0700" }}",
        "status" : "{{ .Params.status }}",
        "description" : "{{ .Params.description }}",
        "cats" : [
            {{ range $index, $e := .Params.cats }}
            {{ if $index }}, {{ end }}
            "{{ .category }}"
            {{ end }}
        ]
        
    }
{{ end }}
]
{{end}}

Output of dist/projects/index.json

{
"data": 
[


{
    "name": "Project 2",
    "location" : "<no value>",
    "website" : "<no value>",
    "logo" : "<no value>"
}

, 
{
    "name": "Project 1",
    "location" : "<no value>",
    "website" : "<no value>",
    "logo" : "<no value>"
}

]
 

}

do you have JSON outputs defined? Something like this??
For better help you shold give us a samle repo :wink:

[outputFormats.JSON]
    MediaType              = "application/json"
    BaseName               = "index"
    IsHTML                 = false
    IsPlainText            = true
    noUgly                 = false
    Rel                    = "alternate"

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

I think the JSON type is built in to Hugo.

Here’s my output config:

[outputs]
  home = ["HTML","JSON"]
  page = ["HTML","JSON"]
  section = ["HTML","JSON"]

Here’s a sample repository. https://github.com/xlegs/hugo-error-demo

There is no usable HTML output here. All I need is to generate correct JSON files.

When I test your site, I can see that it does use the project/list.json.json and universities.list.json layout files appropriately.

Do this to test yourself: add

"layout" : "universities/list"
and
"layout": "projects/list"

to somewhere in the json layout files accordingly. I can see that Hugo uses the appropriate layout files to generate the JSON output.

check

I never used baseof JSON files. If the doc does not have JSON template names, go after RSS etc…
I redefined outputFormats to get the right file names and go around a problem with RSS

layouts/projects/list.json.json and layouts/universities/list.json.json should work for you.

hope this helps

This does not work and results in no change.

Can you provide an example, because the the json files are not correct on my machine.

This is what I did:

I downloaded your site code

In layouts/universities/list.json:

{{ define "main" }}
[
{
    "layout" : "universities/list"
},
{{ range ... the rest of your code 

Output: public/universities/index.json

{
    "data": 
[
{
    "layout" : "universities/list"
},
    {
        "name": "B University",
        "location" : "Belem, Brazil",
        "website" : "https://www.google.com",
        "logo" : "/img/uploaded/logo.png"
    }

In layouts/projects/list.json.json:

{{ define "main" }}
[
{
    "layout": "projects/list.json"
},
{{ range ... the rest of your code

Output: public/projects/index.json:

{
    "data": 
[
{
    "layout": "projects/list.json"
},
    {
        "name": "Project 2",
        "permalink": "https://iajes-frugal.netlify.app/projects/project-2/",
        "university" : "C University",
        "date" : "08 Dec 18 18:14 +0000",
        "status" : "Completed",


Check that you are clearing your public/dist folder in between. Try hugo --cleanDestinationDir

Sometimes it works. Then I stop the server, delete dist, and when I rebuild the problem comes back.

Why is it so inconsistent?