Just to clarify what @Karmalakas says, you need a markdown file under your user/pages directory called myfile.md, just like you would for HTML templates.
Thanks for your friendly response and clarification, Hugh, much appreciated. I’ll give that a try. I assume I have to create the page in the Grav Dashboard, as well? If so, what do I put for “Folder Name” since I want the json file to appear at the root level (ie. localhost/myfile.json).
Just some potentially helpful feedback: It would have helped if the step of putting the page (the /md file) in place were more clear in the docs - Because it currently says, “If your page was defined by a page with filename of blog-item.md, Grav in turn looks for a Twig template called blog-item.html.twig to render the page.” The word “if” threw me off, implying it being optional. Because this is describing a different content type I thought there may be a different way of serving that content type, since it isn’t a “page” but a .json file. Anyway, thanks again, Hugh.
Folder is the path. MD file is the template. You should have something like:
/user/pages/myfilepath/myfiletemplate.md
When you visit /myfilepath, it will try to load myfiletemplate.html.twig by default. If you visit /myfilepath.json, it will try to load myfiletemplate.json.twig
Okay, got it working. Boy, that’s confusing, though. Definitely not what I got from the docs, but maybe that’s just me.
A file called “default.md” was created in the folder when I created the page in the Dashboard, as expected. But I don’t think I can (or should) change that filename, correct? Because it seems to be a pre-set value in the Dashboard template dropdown (and I’m not sure why). So I didn’t change it, even though it would make it more clear if I did change it (say, from default.md to myfile.md).
I changed the twig file to be named default.json.twig in /templates/ (from myfile.json.twig) and it worked, which, to me, is confusing, but I do understand it now.
What if I wanted to serve multiple .json files from the root - then I’d have to change one of those default.md files to be something else, correct? But would that break it in the Dashboard somehow?
Create a page (e.g. /user/pages/05.json/) and set its template to myfile.json.twig in the frontmatter. Then access it via /json and load it with jQuery:
$.getJSON(‘/json’, data => console.log(data));
Alternatively, put a raw .json file in /user/data/ and fetch it directly.