Output Jekyll _data as JSON

I've been playing around with Jekyll for a few days (yup, tried to build my blog in Jekyll; works well but Ruby made a mess of my system; even with Brew). And I started building a proof-of-concept site for a client in Jekyll as a replacement to Wordpress, as wordpress didn't allow them to update their many products very fast (Could take them a whole day).

Anyway, essentially I needed some data to be present in Jekyll's build process and available externally as JSON. Obviously I didn't want to duplicate the data for them to maintain. But Jekyll only really has built in support for outputing JSON for its collections, not the _data.

And I considered building a whole plugin to do it.. then realised "Durrr" I can just use Liquid to Jsonify the _data object and render the file with a JSON extensition. b00m! :D

All i needed to do was create a file at ./assets/json/data.json.html and add the following content:

---
permalink: /assets/json/data.json
---

{{ site.data | jsonify }}

Then when the site compiles, a .json file is created at ./_site/assets/json/data.json with all the data from the files in the _data folder.

The only issue here is the way the data is formed in the _data object might not always output valid JSON, so better to be excplicit about which data file you want based on it's filename, so instead of site.data, I would use site.data.product_info.

I use snake_case here as while I like my files to be named file-name.json, I shouldn't be referencing the data with site.data.file-name, baaaaad. :)

Hope this was useful for someone.