Hugo not generating/populating index.json correctly

Hi, I’m trying to implement a search functionality for my site but I’m having issues generating an index.json file. I’ve looked through multiple tutorials and tried out different templates for the index.json file, but I always get basically just the template as the output.

It doesn’t seem to be an issue with any specific template, all templates that I’ve tried so far are failing. I guess I’m just missing something.

To make things a bit clearer, here is a simplified version of the template I’m trying to use.

{
  {- $list := slice -
  }
}

{
  {- range site.RegularPages.ByPublishDate.Reverse -
  }
}
{
  {- $item := dict
"title" .Title
-
  }
}

{
  {- $list = $list | append $item -
  }
}
{
  {- end -
  }
}

{
  {- $list | jsonify -
  }
}

I have added “JSON” to the outputs of home in my hugo.toml file.

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

To make sure that this actually has content, i tested it by just adding this in the html of a test site and it works - so it doesn’t seem like the problem is with the content.

However, after building, in my public directory the content of the generated index.json file is literally the same as the template in the layouts folder:

{
  {- $list := slice -
  }
}

{
  {- range site.RegularPages.ByPublishDate.Reverse -
  }
}
{
  {- $item := dict
"title" .Title
-
  }
}

{
  {- $list = $list | append $item -
  }
}
{
  {- end -
  }
}

{
  {- $list | jsonify -
  }
}

It seems like it’s not filled/generated correctly.

I’ve tried out several slightly different templates and I’ve tried it out in a new test hugo site - same results every time.

Just to be sure, I also tried to generate the json just with a minimal setup:

{
  "message": "Test output"
}

This is generated correctly.

Here is the output of my hugo env:

hugo v0.136.2+extended linux/amd64 BuildDate=unknown
GOOS="linux"
GOARCH="amd64"
GOVERSION="go1.23.2"
github.com/sass/libsass="3.6.6"
github.com/webmproject/libwebp="v1.3.2"

Maybe just as a note, in my index.json the parts where the hugo template is used are also underlined with red squiggly lines - not sure if this is just an issue with the JSON formatting in my editor or if I’m just tackling this whole thing wrong and am missing something crucial.

Thank you for the help in advance :slight_smile:

Really? Use double brackets, not single brackets.

Hi, I think I am - no? They are just formatted weirdly, they’re on the line before:

{
  {- $list := slice -
  }
}

Or do you mean something else?

{{ and }} without any white space between.

It changes the formatting automatically inside the JSON file, works fine in normal HTML files, I’m using those templates without issues in HTML but I can’t get it to work in the JSON file.

Tried it in both VSCode and Neovim. Is there something additional needed for the template to work in a JSON file?

Don’t format it. It should look like any other template in your project.

Oh god, you’re right! it’s actually the formatting that’s causing the problem - tried to fix it just within normal Notepad and it seems to be working now.

Thank you for the help!

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