You might have data stored in your Jekyll project. Typically in YAML, CSV or JSON file in the _data/
directory. Or in the frontmatter of your pages. This tutorial shows you how to render as JSON pages.
The data files could be:
- in version control (visible in Git and on GitHub)
- dynamic (ignored by Git) - for example if you have a script to fetch API data and write to
_data
every time you deploy your site.
This is useful if you want to make your data available for another service or for others to consume (like GitHub, Twitter and Facebook make their data available on APIs). Or perhaps you have JavaScript single-page application that reads from your API backend to serve the app or build a search index.
Given data in page frontmatter or a YAML data file:
---
my_data:
- a: 1
b: 2
- a: 100
b: 200
---
Or CSV data as:
a, b
1, 2
100,200
The rendered JSON page will look like this:
localhost:4000/foo.json
[
{ "a": "1", "b": "2" },
{ "a": "100", "b": "200" }
]
Note that Jekyll is a static site generator. So your JSON API data will only be updated whenever you make a commit and the site rebuilds. If your API needs to have data which changes based on many users or needs to change in realtime, then you're better off using Node.js or Python for your API.
Here we will use .json
as the file extension, instead of the usual .md
or .html
. This means your HTTP header will be set correctly:
Content-Type application/json
We'll also make sure to set the layout to null
so that we get a pure JSON page without any HTML styling.
We'll use our the YAML data to build the response. If you use the jsonify
Jekyll, you can convert data straight to JSON without worrying about for
loops or JSON syntax.
I recommend using a Jekyll extension for your IDE, to handle Liquid syntax highlighting and recognizing frontmatter. Then make sure you choose jekyll
as your formatter for your .json
files.
It looks like JSON files are excluded from a sitemap file by default. The plugin recognizes just HTML and Markdown files.
See also Data files in the Jekyll docs. You might iterate over data in a JSON or YAML file or files to build up output on a page. The data files are your database and then Jekyll builds your API. This is also a way to build a databse JSON file to be consumed on the frontend such as for search functionality.
Or you might store your data in the frontmatter of Jekyll Collections which get outputted as pages similar to the Page example below. That way you can group your JSON data into collections as interate over them easily.
@MichaelCurrin Thanks. I do have the all posts json just showing what I want:
For the asset paths issue, maybe I'm just approaching this wrong. I'm following the "Posts as JSON Files" section above. However, I don't want to mess with the .md files in my _posts folder, because then the json outputs are displayed on the website index files (https://blog.pinballmap.com/). So, instead I created a separate _json collection containing json versions of the md files (which I made manually, for the most part) and then they get stored safely in here: https://github.com/RyanTG/pbm-jekyll/tree/master/_site/json
I don't have relative_url set, so hopefully that's just my issue!