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.