Unable to sort or fully read data json

For one of my list pages, I’m trying to read data from data/foo.json. The json looks like this.

{
  "metadata": {
    "title": "Lorem ipsum dolor sit amet, consectetur adipisicing elit",
    "full": "Lorem ipsum dolor sit amet, consectetur adipisicing elit",
    "date": "2020-06-15",
    "link": "https://arxiv.org/abs/111"
  },
  "rocker": {
    "title": "Lorem ipsum dolor sit amet, consectetur adipisicing elit",
    "full": "Lorem ipsum dolor sit amet, consectetur adipisicing elit",
    "date": "2020-08-17",
    "link": "https://arxiv.org/abs/1111"
  }
} 

On my list.html, I am trying to read these json entries and sort them.

{{ $json := getJSON "data/foo.json" }}
{{ range sort $json "date" "desc" }}
<!-- TODO: This only returns 5 results. Why??? -->
    <div class="post">
      <h2><a href="#">{{ .title }}</a></h2>
      <p>{{ .full }}</p>
      <div class="multi-links">
        <a href="{{ .link }}" class="link icon-link icon-publication">View Publication</a>
      </div>
    </div>
{{end}}

There are two problems. It doesn’t seem to understand or sort by date.
It also only returns 5 results when the json contains dozens more.

Can someone point me to what I’m doing wrong?

Thanks very much for any leads.

Does using $.Site.Data instead of getJSON change anything? See https://gohugo.io/templates/data-templates/.

Similar outcome, unfortunately

I’m not able to reproduce this. What does hugo version show?

Hugo version is v0.74.3.

Perhaps I’m unclear on the $.Site.Data syntax (Still getting used to Hugo syntax).

Would I use

{{ range  .Site.Data.foo  }}

where the data reside in data/foo.json?
This works, but I am still not able to sort by date or show all entries (it’s still limited to 5).

Big apologies @moorereason

The error was on my part. I was still testing the json approach so the dates were wrong. Once I fixed those, my original approach worked just fine. Thanks very much for your help!

facepalm

The problem with the 5 results only was that a bunch of json entries had the same key and it just picked the last one. This is also fixed.

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