# Help with getJSON: unexpected end of JSON input

**URL:** <https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532>\
**Category:** support\
**Created:** [February 18, 2020, 7:13am UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532 "2020-02-18T07:13:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![aormsby](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/aormsby/32/9872_2.png) [@aormsby](https://discourse.gohugo.io/u/aormsby)\
**Post date:** [February 18, 2020, 7:13am UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/1 "2020-02-18T07:13:43Z")

</div>

I’m having basically the same issue where I want to use `getJSON` in a shortcode to reference a `videos.json` file at a relative URL. Here’s a short recreation.

Directory:

> my-post  
> \_ index.md  
> \_\_ img  
> \_\_\_ videos.json  
> \_\_\_ other image files

I range through the `img` dir and explicitly pick out the `videos.json` file.

```
{{ range .Page.Resources.Match ( printf "%s%s%s" "*/" (.Get "src") "/*") }}
    {{ if eq (path.Base . ) "videos.json" }}
        {{ $videos := getJSON ( . ) }}

```

The same error:

```
Failed to get JSON resource "img/videos.json": unexpected end of JSON input

```

I really wish I understood why.

> (From bep)  
> That said, I would recommend you put these types of JSON files into /data,

While that’s a good solution for many cases, I’m using this to populate an image gallery, and I want to keep the image and video content bundled with the post itself to maintain content portability and ease of use.

Is there anything you might recommend I try here? If I can just get the JSON data to read in properly, I should be fine moving forward.

**EDIT:** Shortly after I posted this I tried using `.Permalink` and that seems to work for the moment. However, I’m unsure if that’s an ideal solution. I’d still like some insight on the proper way to go about this. Also, I’m running all these tests on the local server, and I don’t know if that affects how something like `.RelPermalink` works or (currently) does not work in this environment.

**EDIT 2:** I really just want to be able to use the relative URL that seems to work fine under other circumstances like this.  
`{{ $videos := getJSON ( . ) }}`  
The `.` here would be the resource URL being used inside the range I used above, but it does not work with the `getJSON` function. Is it possible that only absolute site URLs are accepted with this function at this time? It would seem like a bit of an oversight if I couldn’t read in a JSON file using a relative URL.

**EDIT 3:** And now that I’ve realized I need set the json cache to 0 if I ever want it to refresh the data, I have a new problem where starting the local Hugo server actually fails because (I think) it can find neither the previously cached data nor the hosted file (since the host is not yet running). I gotta say, this is starting to look like a hopeless mess.

---

<div class="post-metadata">

**Author:** ![pointyfar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pointyfar/32/5797_2.png) [@pointyfar](https://discourse.gohugo.io/u/pointyfar)\
**Post date:** [February 19, 2020, 10:44am UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/2 "2020-02-19T10:44:21Z")

</div>

Hi,

I’ve moved your question into its own topic as the one you were replying to has been solved, is 2y old, and does not seem to be a similar issue beyond the use of `getJSON`.

It would be easier to get help here if you were to share your site code so that we can replicate your issue. Have a read here about [Requesting Help](https://discourse.gohugo.io/t/requesting-help/9132) .

---

<div class="post-metadata">

**Author:** ![aormsby](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/aormsby/32/9872_2.png) [@aormsby](https://discourse.gohugo.io/u/aormsby)\
**Post date:** [February 28, 2020, 3:56pm UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/3 "2020-02-28T15:56:23Z")

</div>

Well, it started out as the same issue, but it really snowballed the more I experimented with it. In the end, I did get things working eventually, and most of the problem came from misunderstanding some of the ways things worked (along with that somewhat strange caching situation). At the moment, I don’t have my notes in front of me - I may follow up with a working code sample later just to show a solution for others.

What confused me the most was probably how `getJSON` reacted differently to file paths than other functions (such as `fileExists`). They each required a different file path to get the expected results, which would imply they’re starting their search from different locations. While that’s not necessarily a bad thing, it’s also extremely unclear that it’s happening. While I do really enjoy working with Hugo, I do lose time to confusing things like that quite often.

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [February 28, 2020, 8:31pm UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/4 "2020-02-28T20:31:04Z")

</div>

[`fileExists`](https://gohugo.io/functions/fileexists/#readout) looks for files in the local file system

[`getJSON`](https://gohugo.io/templates/data-templates/#call-the-functions-with-a-url) can fetch a data file from the local data folder or from an external URL and it takes variadic arguments.

So these two functions do have different syntax for a good reason.

Also you can always clear the cache by using the [`--ignoreCache`](https://gohugo.io/templates/data-templates/#cache-urls) CLI flag.

---

<div class="post-metadata">

**Author:** ![aormsby](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/aormsby/32/9872_2.png) [@aormsby](https://discourse.gohugo.io/u/aormsby)\
**Post date:** [March 3, 2020, 10:10am UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/5 "2020-03-03T10:10:23Z")

</div>

> [`fileExists`](https://gohugo.io/functions/fileexists/#readout) looks for files in the local file system

That’s all well and good, and I’ve been using it to some extent to get information when using `getJSON`. Unfortunately, the results of the two functions are often unequal. For example, I can easily find a file in a relative position using `fileExists`, but then `getJSON` doesn’t work to retrieve it However, I don’t think this is `fileExists` fault.

Instead, I think the issue may be in this assumption –

> [`getJSON`](https://gohugo.io/templates/data-templates/#call-the-functions-with-a-url) can fetch a data file from the local data folder…

I’m not so sure it can. I have _only_ been successful using getJSON with full URLs, so maybe there is a bug here somewhere. Passing in local directory data - either relative or absolute - does _not_ retrieve the JSON file.

> **Doesn’t work** → (from site root) /projects/armydillos/ArmyDillos-web/Build/ArmyDillos-web.json  
> **Works** → [https://www.adamormsby.com/projects/armydillos/ArmyDillos-web/Build/ArmyDillos-web.json](https://www.adamormsby.com/projects/armydillos/ArmyDillos-web/Build/ArmyDillos-web.json) **OR** [https://localhost:1313/projects/armydillos/ArmyDillos-web/Build/ArmyDillos-web.json](https://localhost:1313/projects/armydillos/ArmyDillos-web/Build/ArmyDillos-web.json)

Oddly enough, `getJSON` functions run during site builds as well. Since I’m using full URLs and can’t use local path data with `getJSON`, I get errors like this one (this JSON file had not been pushed to the external site yet.)

> Building sites … ERROR 2020/03/03 16:48:29 Failed to get JSON resource “[https://www.adamormsby.com/projects/armydillos/ArmyDillos-web/Build/ArmyDillos-web.json](https://www.adamormsby.com/projects/armydillos/ArmyDillos-web/Build/ArmyDillos-web.json)”: Failed to retrieve remote file: Not Found

However, the published site with the new file works fine. If I could just use directory paths, I wouldn’t have this issue.

Is there just something wrong with `getJSON`?

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [March 3, 2020, 10:22am UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/6 "2020-03-03T10:22:10Z")

</div>

Please do share a repo as stated in the [Requesting Help](https://discourse.gohugo.io/t/requesting-help/9132) guidelines.

I am using `getJSON` on a daily basis to render data from JSON files that reside under the `/data/` folder.

Something else is going on that is due to your setup. The function works as intended for me.

P.S. Is your JSON valid? There are online tools to check if it is.

---

<div class="post-metadata">

**Author:** ![aormsby](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/aormsby/32/9872_2.png) [@aormsby](https://discourse.gohugo.io/u/aormsby)\
**Post date:** [March 3, 2020, 10:38am UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/7 "2020-03-03T10:38:49Z")

</div>

Okay, I think we’re not on the same page here.

Yes, my JSON is valid. However, I’m _not_ using the `/data/` folder as I mentioned in my initial post. However, I can see that was easier to understand before the post got moved, so I’ll restate the issue a bit clearer here.

I would like to access `.json` files inside my post bundles. _Not_ the `/data/` folder. My reason for this is that in some cases the post and its data are more easily organized and transferrable this way.

For example, I might have a directory structure like this.

> posts/  
> \_ my-post/  
> \_\_ index.md  
> \_\_\_ subfolder/  
> \_\_\_\_ mydata.json

According to the [`getJSON`](https://gohugo.io/templates/data-templates/#load-local-files) docs, it should be possible to access a local json file (_without_ using `/data/`). However, I can only access the file using a full URL, not a local path.

This is the issue. Something in the way `getJSON` is looks for the path doesn’t work. This is not a case where I want to use the `/data/` folder.

Maybe I should just make a Github issue about it.

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [March 3, 2020, 10:42am UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/8 "2020-03-03T10:42:54Z")

</div>

> [@aormsby](#):
>
> I would like to access `.json` files inside my post bundles. _Not_ the `/data/` folder. My reason for this is that in some cases the post and its data are more easily organized and transferrable this way.

Then `getJSON` will not work as you’ve seen. You are making the call for a file residing in the `contentDir`

As per the Docs link you posted:

> The local CSV files to be loaded using `getCSV` must be located **outside** of the `data` directory.

That is for CSV. Not about JSON.

> [@aormsby](#):
>
> Maybe I should just make a Github issue about it.

What you describe in this topic is a _new_ feature request and _not_ a bug, so do make sure to reflect that if you open an issue on GitHub.

The Docs are clear.

---

<div class="post-metadata">

**Author:** ![pointyfar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pointyfar/32/5797_2.png) [@pointyfar](https://discourse.gohugo.io/u/pointyfar)\
**Post date:** [March 3, 2020, 10:52am UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/9 "2020-03-03T10:52:09Z")

</div>

But why even use `getJSON` at all? Your json file is already in your page bundle, you could just do

```auto
{{ with .Page.Resources.GetMatch $jsonfile }}
  {{ .Content | transform.Unmarshal }}
{{ end }}

```

It would be easier to help if you also mention what you want to accomplish (more than just "I want to `getJSON` a json file-- which is _how_ you are trying to do it.).

---

<div class="post-metadata">

**Author:** ![aormsby](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/aormsby/32/9872_2.png) [@aormsby](https://discourse.gohugo.io/u/aormsby)\
**Post date:** [March 3, 2020, 10:57am UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/10 "2020-03-03T10:57:41Z")

</div>

I’m not sure you’ve read the docs closely enough. Here are the exact words that support my case being valid and explicitly states it should be functionally possible.

![Screen Shot 2020-03-03 at 17.49.34](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/2X/a/a2cce5c4faf601a231c0d2c4d09be913b448ec81.png)

![Screen Shot 2020-03-03 at 17.49.45](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/2X/7/7cdcb844e8c6f5fca0eb61f17fb68dc7dd99819c.png)

---

<div class="post-metadata">

**Author:** ![aormsby](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/aormsby/32/9872_2.png) [@aormsby](https://discourse.gohugo.io/u/aormsby)\
**Post date:** [March 3, 2020, 11:03am UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/11 "2020-03-03T11:03:41Z")

</div>

> [@pointyfar](#):
>
> {{ with .Page.Resources.GetMatch $jsonfile }}  
> {{ .Content | transform.Unmarshal }}  
> {{ end }}

Because honestly, I had no idea I could do that. 😅 I see now that there’s a good page on this in the docs, but I never ran across it since I didn’t know that’s what I was looking for. I’ll give it a shot.

> mention what you want to accomplish

Actually, working with the data has been super easy! I just use it to populate some HTML in my template. Just accessing it in the first place has been an odd challenge, so that’s really my goal at this point.

---

<div class="post-metadata">

**Author:** ![pointyfar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pointyfar/32/5797_2.png) [@pointyfar](https://discourse.gohugo.io/u/pointyfar)\
**Post date:** [March 3, 2020, 11:07am UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/12 "2020-03-03T11:07:53Z")

</div>

BTW,

```auto
{{ $json := getJSON "/content/some/folder/data.json" }}

```

works perfectly fine from within a shortcode. If it does not work for you, there is something wrong with how you are doing things. We can’t help if we can’t replicate your issue.

---

<div class="post-metadata">

**Author:** ![aormsby](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/aormsby/32/9872_2.png) [@aormsby](https://discourse.gohugo.io/u/aormsby)\
**Post date:** [March 3, 2020, 11:34am UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/13 "2020-03-03T11:34:56Z")

</div>

Yes, that’s correct on a local build. Unfortunately, there is no `/content/` directory in the `/public/` folder of the site after you build it, so that path wouldn’t exist in the live site directories. So wouldn’t that only work on a local build then?

I’m setting up a test case, and I’ll post when available.

**EDIT:**  
Unless I’ve read the docs header ‘Load Local Files’ and incorrectly misinterpreted it to mean ‘Load files in the relative directories without a full URL’. That would certainly change my perspective on things.

---

<div class="post-metadata">

**Author:** ![aormsby](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/aormsby/32/9872_2.png) [@aormsby](https://discourse.gohugo.io/u/aormsby)\
**Post date:** [March 3, 2020, 11:45am UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/14 "2020-03-03T11:45:35Z")

</div>

Which statement is more accurate?

A) `getJSON` is meant to be used on my local file directory, not a built site.  
B) `getJSON` can be used to access local files on a built, published site.

---

<div class="post-metadata">

**Author:** ![pointyfar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pointyfar/32/5797_2.png) [@pointyfar](https://discourse.gohugo.io/u/pointyfar)\
**Post date:** [March 3, 2020, 1:47pm UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/15 "2020-03-03T13:47:51Z")

</div>

I don’t understand what your question is.

`getJSON` does its “magic” while your site is being built. Once your site is built, `getJSON` has nothing to do with it anymore. You will have your static pages with whatever information you pulled out of your json.

Do not think of it like jQuery `getJSON`, which does the `get`-ing after the site is built.

---

<div class="post-metadata">

**Author:** ![gosho](https://avatars.discourse-cdn.com/v4/letter/g/e0b2c6/32.png) [@gosho](https://discourse.gohugo.io/u/gosho)\
**Post date:** [March 3, 2020, 9:58pm UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/16 "2020-03-03T21:58:25Z")

</div>

That’s a nice one.  
However, given that content directory is configurable and not necessarily in hugo working directory, is there any function at all that can be used to fetch content that does to reside in hugo?

Some context: I have distinct repositories/direcotries for the hugo generator and the content. I do a pre-build step with nodejs and fetch commits jsons from github for each .md because the gitinfo object scenario is too limited for my case. The jsons are stored side-by-side with the mds in the content directory. Then I get them with `GetJSON` in several partials. And it was fine until the moment I decided to instead of copy the content in hugo to employ `--contentDir` flag and use it from where it resides.

Part of the problem is how do you dynamically supply the name of the content directory to such function if that is necessary for it to function properly? In the case of `GetJSON` I had to start my paths with `content`, but as soon as I considered using `--contentDir` referring to an external directory, it started to fail for obvious reasons. And there seems to be no way of telling the content directory dynamically, making the code quite dependable on env changes with hardcoded paths here and there.

I suppose that instead of storing the jsons i want to read side-by-side with the markdowns they correspond to, I could store them in the `data` folder, making them available to `GetJSON` on a well-known base path. But I was hoping for a less labor intensive solution coming directly from Hugo.

The constraints we discuss here if we use `--contentDir` referencing a directory outside hugo are not only on `GetJSON`. `ReadDir` for example is in the same boat. Perhaps the documentation could emphasize more on that?

---

<div class="post-metadata">

**Author:** ![maiki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/maiki/32/16384_2.png) [@maiki](https://discourse.gohugo.io/u/maiki)\
**Post date:** [March 3, 2020, 10:40pm UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/17 "2020-03-03T22:40:39Z")

</div>

> [@aormsby](#):
>
> Which statement is more accurate?

A: local file, not a built site.

I wish I could definitively say all the ways `getJSON` works, but I only use it to pull in data from remote, public URLs (I search for and show recent commits from hundreds of repos, and for related posts in some forums).

When I build the site–including `hugo server`–`getJSON` goes and grabs the resource at the URL and caches it for access. This of course makes it possible to link to a resource generated by the site itself, from it’s published form online, but I try to avoid those kinds of data loops, because one cache clearing screws everything up.

---

<div class="post-metadata">

**Author:** ![maiki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/maiki/32/16384_2.png) [@maiki](https://discourse.gohugo.io/u/maiki)\
**Post date:** [March 3, 2020, 10:45pm UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/18 "2020-03-03T22:45:37Z")

</div>

> [@gosho](#):
>
> I do a pre-build step with nodejs and fetch commits jsons from github for each .md because the gitinfo object scenario is too limited for my case.

Which scenario are you referring? I pull in commits for a few hundred repos from a self-hosted gitea instance, and of all my `getJSON` uses that has been the least difficult to get working.

---

<div class="post-metadata">

**Author:** ![aormsby](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/aormsby/32/9872_2.png) [@aormsby](https://discourse.gohugo.io/u/aormsby)\
**Post date:** [March 4, 2020, 2:20am UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/19 "2020-03-04T02:20:08Z")

</div>

> [@pointyfar](#):
>
> `getJSON` does its “magic” while your site is being built. Once your site is built, `getJSON` has nothing to do with it anymore.

Ah okay, that’s the part I’ve misunderstood this whole time. I had definitely been thinking of it as something like jQuery’s function like you said. Knowing this, I could likely still use it for my purposes since I just want some static data to be dropped from the JSON files into a template.

However, you also mentioned `transform.Unmarshal` as an alternative (which does work fine). If they’re both only working during the build, what would be the benefits/drawbacks of choosing either one?

I suppose as maiki stated, the cache clearing issue might be avoided by using the resource matching with `Unmarshal` in this scenario.

> [@maiki](#):
>
> When I build the site–including `hugo server` – `getJSON` goes and grabs the resource at the URL and caches it for access. This of course makes it possible to link to a resource generated by the site itself, from it’s published form online, but I try to avoid those kinds of data loops, because one cache clearing screws everything up.

---

<div class="post-metadata">

**Author:** ![pointyfar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pointyfar/32/5797_2.png) [@pointyfar](https://discourse.gohugo.io/u/pointyfar)\
**Post date:** [March 4, 2020, 3:17am UTC](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532/20 "2020-03-04T03:17:14Z")

</div>

> [@gosho](#):
>
> However, given that content directory is configurable and not necessarily in hugo working directory, is there any function at all that can be used to fetch content that does to reside in hugo?

You are probably better off asking your question on a separate topic, with a sample repo attached.

[Next page](https://discourse.gohugo.io/t/help-with-getjson-unexpected-end-of-json-input/23532.md?page=2)
