HUGO

getJSON nested (gitHub calls)

I’m making nested getJSON calls and using range to get at the data therein, and am receiving errors that I can not figure for the life of me. Quite possible that I am missing something glaring since I’ve been up for ~40 hours, so I may just need a second pair of eyes here:

{{ $urlPre := "https://api.github.com" }}
{{ $git_user := "XXX" }}
{{ $user_json := getJSON $urlPre "/users/" $git_user "/repos" }}
{{ range $user_json }}
	{{ if .name }}
	name={{ .name }}
		{{ $repo_json := getJSON $urlPre "/repos/" $git_user "/" .name }}
		{{ range $repo_json }}
			<a href="{{ .html_url }}">{{ .name }}</a> has {{ .watchers }}
		{{ end }}
			
	{{ end }}
{{ end }}

upon running hugo yields:

ERROR: 2015/11/10 template: partials/codelist.html:9:15: executing "partials/codelist.html" at <.html_url>: can't evaluate field html_url in type interface {} in partials/codelist.html

However the repo_json is properly set and does contain html_url. So I am at a loss. Yes, the below does the same I know, but I was using the above to test for future stuff such as looking of builds and download stats. Any ideas?

{{ $urlPre := "https://api.github.com" }}
{{ $git_user := "XXX" }}
{{ $user_json := getJSON $urlPre "/users/" $git_user "/repos" }}
{{ $name := "" }}
{{ range $user_json }}
	<a href="{{ .html_url }}">{{ .name }}</a> has {{ .watchers }}
{{ end }}

Hot tip:

https://gohugo.io/templates/debugging/

The dot is not always what you think it is.

Well there are no KEYS returned it would appear. This looks to be a bug in Hugo. I ran the JSON output from the API call to GitHub through a JSON validator and it is valid. Unless I am missing something, this is a fault within Hugo?

Hugo uses the JSON decoder from GO’s stdlib, and that should be stable enough …

@SchumacherFM wrote the GetJSON, I think – he may chime in.

I have to test that and try to reproduce the error. may take couple of days.

Sure thing!

To eliminate variables (quite literally) I simplified the code ( you can even use my github and repo if you want an exact reproduction)

{{ $user_json := getJSON "https://api.github.com/repos/variab1e/dnsmasq-qpkg" }}
{{ range $user_json }}

			{{ printf "v=%#v" . }}

{{ end }}

This is neither a bug of Hugo nor of Go.

You simply try to iterate over a non existing array in the JSON and then of course it breaks. Please have a look in the JSON you try to load.

here is a working example:

{{ $urlPre := "https://api.github.com" }}
{{ $git_user := "userXYZ" }}
{{ $user_json := getJSON $urlPre "/users/" $git_user "/repos" }}
{{ range $user_json }}
  {{ $u := . }}
 <h2>{{ $u.name }}</h2>
  {{ $repo_json := getJSON $urlPre "/repos/" $git_user "/" $u.name }}
  {{ range $k,$v :=   $repo_json }}
   {{ $k }} =&gt; {{ $v }}<br><br>
 {{ end }}
  <hr>
{{ end }}

Also GitHub provides a lot of data redundant so the 2nd call to getJSON can be avoided.

2 Likes

I know this is an old post, but I’d like to thank @SchumacherFM for this code and ask a question about this: when I use a wrong user name on Github, their API returns an error, and Hugo reacts with:

ERROR 2020/02/11 09:56:19 Failed to get JSON resource "https://api.github.com/users/santa-clauz": Failed to retrieve remote file: Not Found

[That error occurs because santa-clauz doesn’t exist. That’s right guys, sorry about that]

My question, is how can I catch that without breaking everything? I want to display some error message, not have my site stop building just because some Github user was deleted…

Thanks in advance

Click to see full shortcode
{{/*
--------------------
Shortcode usage:

{% ghcontributors user1 user2 user3 %}}

(You can use an indefinite number of parameters)
-------------------
*/}}

<style type="text/css">
.ghContributors {
  display:flex;
  flex-flow: row wrap;
  align-content: flex-start;
}
.ghContributors > div {
  display: inline-flex;
  margin: 5px 5px;
}
</style>
<div class="ghContributors">
{{ range .Params }}
  {{ $git_user := . }}
  {{ $u := getJSON "https://api.github.com/users/" $git_user }}
<div>
    <div style="float:top;">
      <a href="{{$u.avatar_url}}" data-featherlight="image">
      <img src="{{$u.avatar_url}}" class="inline" style="width: 50px;height: 50px;margin-bottom:.25em; vertical-align:middle;">
    </a>
    <div><a href="{{$u.html_url}}"  class="highlight">{{$u.login}}</a></div>
    </div>
  </div>
{{ end }}
</div>