Notenik
Do More With Your Notes

Adventures with Notenik

Adding a JSON Feed to Your Site

Adventure # 5 • Sunday, July 7, 2024

A JSON feed is another kind of web feed that you can use on your site. This format is newer than the XML-based formats, and is considered more modern.

Here is an example of a Merge Template one might use to generate a JSON feed.

<?output "../feed.json"?>
{
    "version": "https://jsonfeed.org/version/1.1",
    "title": "Adventures with Notenik",
    "home_page_url": "https://notenik.app/",
    "feed_url": "https://notenik.app/feed.json",
    "description": "The Latest Adventures with the Mac App Notenik",
    "authors": [
    	{
    		"name": "Herb Bowie and the Notenik Community"
    	}
    ],
    "language": "en-US",
    "items": [
<?nextrec?>
        {
            "id": "notenik.app/adventures/=$date&yyyy-MM-dd$=-=$title&f$=",
            "url": "https://notenik.app/adventures/=$date&yyyy-MM-dd$=-=$title&f$=.html",
						"title": "=$title$=",
<?if "=$imagelink$=" ?>
						"image": "=$imagelink$=",
<?endif?>
						"content_html": "=$body&w1o\$=",
						"date_published": "=$date&yyyy-MM-ddTHH:mm:ssZ$="
<?if =$data-count$= < =$data-max$= ?>
        },
<?else?>
        }
<?endif?>
<?loop?>
    ]
}

Notice the use of the Prep for JSON variable modifier (a backslash) to make sure that your content is formatted to be conform to the requirements for representation of a string within JSON.

Note also the if test near the end comparing the datacount variable to the datamax variable. These are both Merge Template System Variables introduced in Version 14.8.0. The JSON syntax requires a comma following an item in a series, but forbids a trailing comma after the last item. This test ensures conformity to this element of the specification.

One would then want to include a line such as the following within the head section of the HTML pages on your site.

<link rel="alternate" type="application/feed+json" title="JSON feed for Adventures with Notenik" href="feed.json" />

tags: , , ,

Prior Adventure: Adding an Atom Feed to Your Site | 6 Jul, 2024

Latest EntriesArchive by Tag