# Loading JSON from GitHub

**URL:** <https://discourse.gohugo.io/t/loading-json-from-github/51683>\
**Category:** support\
**Tags:** remote\
**Created:** [September 26, 2024, 12:10pm UTC](https://discourse.gohugo.io/t/loading-json-from-github/51683 "2024-09-26T12:10:06Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [September 26, 2024, 12:10pm UTC](https://discourse.gohugo.io/t/loading-json-from-github/51683/1 "2024-09-26T12:10:06Z")

</div>

I am sure this problem will have surfaced already, but I don’t know what exact terms to search for and my searches didn’t yield any results. I am trying to load a JSON file from Github. Those files are delivered as plain/text if I am right and I am receiving the error:

```auto
ERROR error calling resources.GetRemote: failed to resolve media type for remote resource "https://raw.githubusercontent.com/ai-robots-txt/ai.robots.txt/refs/heads/main/robots.json"

```

The layout (just trying to get it and debug it for now:

```gohtml
{{ $data := dict }}
{{ $url := "https://raw.githubusercontent.com/ai-robots-txt/ai.robots.txt/refs/heads/main/robots.json" }}
{{ with resources.GetRemote $url }}
  {{ with .Err }}
    {{ errorf "%s" . }}
  {{ else }}
    {{/* {{ $data = . | transform.Unmarshal }} */}}
    {{ warnf "%#v" $data }}
  {{ end }}
{{ else }}
  {{ errorf "Unable to get remote resource %q" $url }}
{{ end }}

```

I can see the section in the docs talking about [content type issues](https://gohugo.io/functions/resources/getremote/#remote-data) but I fail to understand or apply it here. I am using the same code, but as I wrote above, I am pretty sure it’s plaintext, not octet-stream or similar. The error occurs with and without that commented `transform.Unmarshal` line.

What am I overlooking? Basically, how can I retrieve a remote plaintext and then transform it into JSON?

---

<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:** [September 26, 2024, 12:19pm UTC](https://discourse.gohugo.io/t/loading-json-from-github/51683/2 "2024-09-26T12:19:35Z")

</div>

> [@davidsneighbour](#):
>
> ```auto
> ERROR error calling resources.GetRemote: failed to resolve media type for remote resource "https://raw.githubusercontent.com/ai-robots-txt/ai.robots.txt/refs/heads/main/robots.json"
> 
> ```

I think what you see can be fixed following the note in [resources.GetRemote](https://gohugo.io/functions/resources/getremote/#remote-data)

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [September 26, 2024, 12:30pm UTC](https://discourse.gohugo.io/t/loading-json-from-github/51683/3 "2024-09-26T12:30:48Z")

</div>

That’s what I mean with the section about content type issues. I end up in the error section before that line:

```auto
{{ $url := "https://raw.githubusercontent.com/ai-robots-txt/ai.robots.txt/refs/heads/main/robots.json" }}
{{ with resources.GetRemote $url }}
  {{ with .Err }}
    {{ warnf "blabla" .}}
    {{ errorf "%s" . }}
  {{ else }}
    {{ $data := .Content | transform.Unmarshal }}
    {{ warnf "%#v" $data }}
  {{ end }}
{{ else }}
  {{ errorf "Unable to get remote resource %q" $url }}
{{ end }}

```

CLI prints the blabla. I am not getting to the point where I can do something to the retrieved source.

There is probably a way to tell resource.GetRemote what content format to expect. Or the link to the raw files on Github is not supposed to be used.

---

<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:** [September 26, 2024, 1:05pm UTC](https://discourse.gohugo.io/t/loading-json-from-github/51683/4 "2024-09-26T13:05:05Z")

</div>

> [@davidsneighbour](#):
>
> There is probably a way to tell resource.GetRemote what content format to expect.

This has been discussed, but there’s no option for this.

We trust that the server Content-Type is correct.

> Or the link to the raw files on Github is not supposed to be used.

GitHub has a API that I’m sure reports correct mime types.

---

<div class="post-metadata">

**Author:** ![McShelby](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mcshelby/32/15112_2.png) [@McShelby](https://discourse.gohugo.io/u/McShelby)\
**Post date:** [September 26, 2024, 1:24pm UTC](https://discourse.gohugo.io/t/loading-json-from-github/51683/5 "2024-09-26T13:24:05Z")

</div>

Changing the URL to this [https://api.github.com/repos/ai-robots-txt/ai.robots.txt/contents/robots.json](https://api.github.com/repos/ai-robots-txt/ai.robots.txt/contents/robots.json) shows the correct mime type in the browsers dev tools. Maybe this works too for `GetRemote`

But it seems, the file content is wrapped inside the received JSON.

Taken from GitHubs documentation at [REST API endpoints for repository contents - GitHub Docs](https://docs.github.com/en/rest/repos/contents?apiVersion=2022-11-28#get-repository-content)

---

<div class="post-metadata">

**Author:** ![McShelby](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mcshelby/32/15112_2.png) [@McShelby](https://discourse.gohugo.io/u/McShelby)\
**Post date:** [September 26, 2024, 2:21pm UTC](https://discourse.gohugo.io/t/loading-json-from-github/51683/6 "2024-09-26T14:21:42Z")

</div>

There is an almost similar URL in the docs example for [Base64Decode](https://gohugo.io/functions/encoding/base64decode/)

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [September 26, 2024, 2:29pm UTC](https://discourse.gohugo.io/t/loading-json-from-github/51683/7 "2024-09-26T14:29:36Z")

</div>

@davidsneighbour This works fine for me on both Ubuntu and Windows:

```plaintext
{{ $url := "https://raw.githubusercontent.com/ai-robots-txt/ai.robots.txt/refs/heads/main/robots.json" }}
{{ $r := resources.GetRemote $url }}

```

No errors. Can you test on another platform?

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [September 26, 2024, 2:47pm UTC](https://discourse.gohugo.io/t/loading-json-from-github/51683/8 "2024-09-26T14:47:09Z")

</div>

I am on Ubuntu, and it returns `error calling resources.GetRemote: failed to resolve media type for remote resource &#34;https://api.github.com/repos/ai-robots-txt/ai.robots.txt/contents/robots.json&#34;` if I don’t check for an error:

```auto
{{ $url := "https://raw.githubusercontent.com/ai-robots-txt/ai.robots.txt/refs/heads/main/robots.json" }}
{{ with resources.GetRemote $url }}
{{ . }}
{{ end }}

```

The API way with $url being `https://api.github.com/repos/ai-robots-txt/ai.robots.txt/contents/robots.json` also results in the error.

I am getting more and more convinced that I have a weird setup issue going on. Loading all those URLs in a browser results in JSON and proper content types.

I’ll try my luck adding these as modules I guess.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [September 26, 2024, 2:48pm UTC](https://discourse.gohugo.io/t/loading-json-from-github/51683/9 "2024-09-26T14:48:21Z")

</div>

Have you tried testing on a different platform, or in a virgin VM?

Have you defined or redefined any mediaTypes in your site configuration?

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [September 27, 2024, 10:41am UTC](https://discourse.gohugo.io/t/loading-json-from-github/51683/10 "2024-09-27T10:41:14Z")

</div>

> [@jmooring](#):
>
> Have you defined or redefined any mediaTypes in your site configuration?

That seems to be the reason. I am getting results when I am running the layout on an empty site without other modules. I’ll investigate and write up what I did wrong or what interfered.

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [September 27, 2024, 11:50am UTC](https://discourse.gohugo.io/t/loading-json-from-github/51683/11 "2024-09-27T11:50:44Z")

</div>

My JSONFeed was the culprit.

I have a module that offers [feeds of all kinds](https://github.com/davidsneighbour/hugo-modules/tree/main/modules/feeds) (atom, rss, json). The configuration of the [jsonfeed](https://www.jsonfeed.org/) looked like this:

```toml
[mediatypes]

[mediatypes.'application/feed+json']
suffixes = ['json']

[outputformats]
[outputformats.jsonfeed]
mediaType = "application/feed+json"
path = ""
baseName = "feed"
rel = "alternate"
protocol = ""
isPlainText = true
isHTML = false
noUgly = false
permalinkable = false

```

Apparently Hugo did not like that I took over the .json suffix and handled all JSON from here with `application/feed+json`? After commenting those lines out `resources.GetRemote` magically started working. I have now removed the new mediatype and moved to `mediaType = "application/json"` for the outputformat, using the internal one.

That works for now, but won’t fix the issue at hand: The feed needs to be delivered as `application/feed+json` AND with `.json` as extension and I have a feeling that once `.json` is taken it won’t be available for any other mediatype?

Hugo does not much in relation to delivering the file from the browser to the user so a quick solution is probably to just output it as whatever and then regulate the media type via server configuration.

I stumbled over more issues in my understanding of custom mediatypes. I guess I’ll re-read large parts of the docs.

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [September 29, 2024, 11:50am UTC](https://discourse.gohugo.io/t/loading-json-from-github/51683/12 "2024-09-29T11:50:50Z")

</div>

Closed on September 29, 2024, 11:50am UTC

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