# getJSON nested (gitHub calls)

**URL:** <https://discourse.gohugo.io/t/getjson-nested-github-calls/2066>\
**Category:** support\
**Created:** [November 10, 2015, 12:08pm UTC](https://discourse.gohugo.io/t/getjson-nested-github-calls/2066 "2015-11-10T12:08:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![variable](https://avatars.discourse-cdn.com/v4/letter/v/cab0a1/32.png) [@variable](https://discourse.gohugo.io/u/variable)\
**Post date:** [November 10, 2015, 12:08pm UTC](https://discourse.gohugo.io/t/getjson-nested-github-calls/2066/1 "2015-11-10T12:08:23Z")

</div>

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 }}
```

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [November 10, 2015, 5:58pm UTC](https://discourse.gohugo.io/t/getjson-nested-github-calls/2066/2 "2015-11-10T17:58:38Z")

</div>

Hot tip:

[https://gohugo.io/templates/debugging/](https://gohugo.io/templates/debugging/)

The dot is not always what you think it is.

---

<div class="post-metadata">

**Author:** ![variable](https://avatars.discourse-cdn.com/v4/letter/v/cab0a1/32.png) [@variable](https://discourse.gohugo.io/u/variable)\
**Post date:** [November 10, 2015, 10:45pm UTC](https://discourse.gohugo.io/t/getjson-nested-github-calls/2066/3 "2015-11-10T22:45:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [November 11, 2015, 10:53am UTC](https://discourse.gohugo.io/t/getjson-nested-github-calls/2066/4 "2015-11-11T10:53:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SchumacherFM](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/schumacherfm/32/231_2.png) [@SchumacherFM](https://discourse.gohugo.io/u/SchumacherFM)\
**Post date:** [November 11, 2015, 1:03pm UTC](https://discourse.gohugo.io/t/getjson-nested-github-calls/2066/5 "2015-11-11T13:03:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![variable](https://avatars.discourse-cdn.com/v4/letter/v/cab0a1/32.png) [@variable](https://discourse.gohugo.io/u/variable)\
**Post date:** [November 11, 2015, 1:51pm UTC](https://discourse.gohugo.io/t/getjson-nested-github-calls/2066/6 "2015-11-11T13:51:46Z")

</div>

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 }}
```

---

<div class="post-metadata">

**Author:** ![SchumacherFM](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/schumacherfm/32/231_2.png) [@SchumacherFM](https://discourse.gohugo.io/u/SchumacherFM)\
**Post date:** [November 11, 2015, 6:49pm UTC](https://discourse.gohugo.io/t/getjson-nested-github-calls/2066/7 "2015-11-11T18:49:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pgr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pgr/32/5329_2.png) [@pgr](https://discourse.gohugo.io/u/pgr)\
**Post date:** [February 11, 2020, 10:20am UTC](https://discourse.gohugo.io/t/getjson-nested-github-calls/2066/8 "2020-02-11T10:20:56Z")

</div>

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**
>
> ```auto
> {{/*
> --------------------
> 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>
> 
> ```
