Sometimes you need to load an external JSON resource and display a value for a specific key. Let say by example you want to display the number of stargazers from your Github project. Github expose an API endpoint which return a JSON document with a lot of useful informations about your project.
https://api.github.com/repos/:account/:project
If you run a GET method on https://api.github.com/repos/solisoft/fasty then you'll get a JSON document looking like :
{
id: 188538881,
node_id: "MDEwOlJlcG9zaXRvcnkxODg1Mzg4ODE=",
name: "fasty",
full_name: "solisoft/fasty",
private: false,
owner: {
login: "solisoft",
id: 6237,
node_id: "MDQ6VXNlcjYyMzc=",
avatar_url: "https://avatars1.githubusercontent.com/u/6237?v=4",
gravatar_id: "",
url: "https://api.github.com/users/solisoft",
html_url: "https://github.com/solisoft",
followers_url: "https://api.github.com/users/solisoft/followers",
following_url: "https://api.github.com/users/solisoft/following{/other_user}",
gists_url: "https://api.github.com/users/solisoft/gists{/gist_id}",
starred_url: "https://api.github.com/users/solisoft/starred{/owner}{/repo}",
subscriptions_url: "https://api.github.com/users/solisoft/subscriptions",
organizations_url: "https://api.github.com/users/solisoft/orgs",
repos_url: "https://api.github.com/users/solisoft/repos",
events_url: "https://api.github.com/users/solisoft/events{/privacy}",
received_events_url: "https://api.github.com/users/solisoft/received_events",
type: "User",
site_admin: false
},
html_url: "https://github.com/solisoft/fasty",
description: "Fasty is a new CMS based on openresty / lapis & arangoDB",
fork: false,
url: "https://api.github.com/repos/solisoft/fasty",
forks_url: "https://api.github.com/repos/solisoft/fasty/forks",
keys_url: "https://api.github.com/repos/solisoft/fasty/keys{/key_id}",
collaborators_url: "https://api.github.com/repos/solisoft/fasty/collaborators{/collaborator}",
teams_url: "https://api.github.com/repos/solisoft/fasty/teams",
hooks_url: "https://api.github.com/repos/solisoft/fasty/hooks",
issue_events_url: "https://api.github.com/repos/solisoft/fasty/issues/events{/number}",
events_url: "https://api.github.com/repos/solisoft/fasty/events",
assignees_url: "https://api.github.com/repos/solisoft/fasty/assignees{/user}",
branches_url: "https://api.github.com/repos/solisoft/fasty/branches{/branch}",
tags_url: "https://api.github.com/repos/solisoft/fasty/tags",
blobs_url: "https://api.github.com/repos/solisoft/fasty/git/blobs{/sha}",
git_tags_url: "https://api.github.com/repos/solisoft/fasty/git/tags{/sha}",
git_refs_url: "https://api.github.com/repos/solisoft/fasty/git/refs{/sha}",
trees_url: "https://api.github.com/repos/solisoft/fasty/git/trees{/sha}",
statuses_url: "https://api.github.com/repos/solisoft/fasty/statuses/{sha}",
languages_url: "https://api.github.com/repos/solisoft/fasty/languages",
stargazers_url: "https://api.github.com/repos/solisoft/fasty/stargazers",
contributors_url: "https://api.github.com/repos/solisoft/fasty/contributors",
subscribers_url: "https://api.github.com/repos/solisoft/fasty/subscribers",
subscription_url: "https://api.github.com/repos/solisoft/fasty/subscription",
commits_url: "https://api.github.com/repos/solisoft/fasty/commits{/sha}",
git_commits_url: "https://api.github.com/repos/solisoft/fasty/git/commits{/sha}",
comments_url: "https://api.github.com/repos/solisoft/fasty/comments{/number}",
issue_comment_url: "https://api.github.com/repos/solisoft/fasty/issues/comments{/number}",
contents_url: "https://api.github.com/repos/solisoft/fasty/contents/{+path}",
compare_url: "https://api.github.com/repos/solisoft/fasty/compare/{base}...{head}",
merges_url: "https://api.github.com/repos/solisoft/fasty/merges",
archive_url: "https://api.github.com/repos/solisoft/fasty/{archive_format}{/ref}",
downloads_url: "https://api.github.com/repos/solisoft/fasty/downloads",
issues_url: "https://api.github.com/repos/solisoft/fasty/issues{/number}",
pulls_url: "https://api.github.com/repos/solisoft/fasty/pulls{/number}",
milestones_url: "https://api.github.com/repos/solisoft/fasty/milestones{/number}",
notifications_url: "https://api.github.com/repos/solisoft/fasty/notifications{?since,all,participating}",
labels_url: "https://api.github.com/repos/solisoft/fasty/labels{/name}",
releases_url: "https://api.github.com/repos/solisoft/fasty/releases{/id}",
deployments_url: "https://api.github.com/repos/solisoft/fasty/deployments",
created_at: "2019-05-25T08:09:55Z",
updated_at: "2020-08-26T14:52:47Z",
pushed_at: "2020-08-26T14:52:44Z",
git_url: "git://github.com/solisoft/fasty.git",
ssh_url: "[email protected]:solisoft/fasty.git",
clone_url: "https://github.com/solisoft/fasty.git",
svn_url: "https://github.com/solisoft/fasty",
homepage: "",
size: 2740,
stargazers_count: 100,
watchers_count: 100,
language: "JavaScript",
has_issues: true,
has_projects: true,
has_downloads: true,
has_wiki: true,
has_pages: false,
forks_count: 7,
mirror_url: null,
archived: false,
disabled: false,
open_issues_count: 0,
license: {
key: "mit",
name: "MIT License",
spdx_id: "MIT",
url: "https://api.github.com/licenses/mit",
node_id: "MDc6TGljZW5zZTEz"
},
forks: 7,
open_issues: 0,
watchers: 100,
default_branch: "master",
temp_clone_token: null,
network_count: 7,
subscribers_count: 9
}
There is a specific key called stargazers_count which contains then number of stargazers for this project.
Now if you want to get and display this value you have to write : {{ json | https://api.github.com/repos/solisoft/fasty | stargazers_count }}
You can also use nested keys like owner.login
Adding this feature on FastyCMS was very easy and we wanted to show you the code which handle that :
if action == 'json'
output = from_json(http_get(item))
output = output[v] for k, v in pairs(stringy.split(dataset, "."))
Yes only 3 lines of moonscript ... Awesome :)
If you want to see it in action, just have a look to our main page! The stargazers count is displayed on it.