Building JSON feed

JSON is an open standard file format, which has been in existence since the early 2000s. Created by Douglas Crockford, it has a simple, dictionary-like structure, e.g.,

{
    "key" : "value",
}

In 2017, two entrepreneurs developed a JSON feed spec., leveraging the simplicity of JSON format. The spec is a lightweight alternative to XML-heavy formats (like RSS or Atom), and despite its recency, it enjoys mainstream support across feed reader apps.

The first I wrote a template to produce JSON feed for this web site was in 2020. Over the next few years, I dismantled the Atom feed, leaving only JSON feed as the channel to alert the subscriber of a new note, whenever I produced one.

While the spec allows html content, using content_text I think is more appropriate for a notification system. This is aside from being simpler and readable, and so that is what I use. Jinja, the templating engine I use, has some nice filters (viz., tojson, truncate, and striptags), making it easy to produce a JSON feed.

The template to produce a valid JSON feed for Chisel is as follows.

{% from 'sitesettings.j2' import site_title, site_author, site_url, site_desc -%}
{
  "version" : "https://jsonfeed.org/version/1.1",
  "user_comment" : "This feed lets you know when a new note is published. Copy the following URL -- {{ site_url }}/feed.json -- and add it to your feed reader.",
  "title" : "{{ site_author }}",
  "home_page_url" : "{{ site_url }}",
  "feed_url" : "{{ site_url }}/feed.json",
  "description" : "{{ site_desc | title }}",
  "authors" : [
    {
      "name" : "{{ site_author }}",
      "url" : "{{ site_url }}"
    }
  ],
  "icon" : "{{ site_url }}/inc/apple-touch-icon.png",
  "favicon" : "{{ site_url }}/inc/apple-touch-icon.png",
  "items": [
  {%- for entry in entries %}
    {
      "id" : "{{ site_url }}/{{ entry.url }}",
      "title" : "{{ entry.title }}",
      "url" : "{{ site_url }}/{{ entry.url }}",
      "content_text" : {{ entry.content|truncate|striptags|tojson }},
      "date_published" : "{{ entry.feed_date }}"
    }{%- if not loop.last %},{%- endif %}
  {%- endfor %}    
  ]
}