How to use JSON to send large amounts of data to KLWP

In a previous post I went over over how awesome JSON is as a tool for Tasker. That post laid the groundwork for talking about something I briefly mentioned in my first post about Kustom Live Wallpaper Maker: using JSON to send large amounts of data to KLWP (and presumably its sibling, Kustom Widget Maker/KWGT).

KLWP has a Tasker plugin, but this plugin is basic to say the least. It lets you set a KLWP variable, and that’s it. It’s very useful for some things, like toggles or short pieces of information, but for large arrays and whatnot it’s not ideal. Luckily, KLWP has a wget feature that allows it to grab information from a lot of different web sources, like HTML, RSS (XML), and yes, JSON.

The quick explanation basically ends there — if you know how to use Tasker (or something else) to create JSON data (see the post I linked in the beginning), then using KLWP’s wget feature to access that local JSON file is the other half of the equation.

For the longer explanation, let’s start with the KLWP wget formula syntax, as per the KLWP help pages.

wg([url], filter, params)

For JSON specifically, the syntax is

wg(url, json, JSONPath)

The URL here can be a local path. The filter is always json, to indicate what data type you’re accessing. The parameter is a JSONPath expression, which means you input a path to the fields you want, same as in AutoTools JSON Read that I cover in the Tasker JSON article.

Simple formulas using JSON data

To give you a practical example, the screenshot shows an example of where I use wget to access a Tasker-created JSON file. It shows a screenshot of the KLWP wallpaper that uses the data, the formula, and part of the Tasker-created JSON.

json formula - for some reason we don't have an alt tag here

This is for a Trakt widget I made for KLWP, letting me see upcoming TV episodes based on my Trakt profile. The KLWP formula is for the thumbnail image, and uses the syntax mentioned above. The “URL” (path to local file) and JSON filter are self explanatory, leaving the JSONPath expression at the end:

“.episodes[“ + gv(number)+ “].url”

This is actually a JSONPath expression that includes a KLWP global variable. Without that, the JSONPath expression would simply be .episodes[X].url, where X is the array index number. “episodes” is the name of the JSON object that has all the episode information, and is an array containing a subset of objects detailing information about episode title, episode number, and so on — including “url”, which is the URL to a thumbnail for the show’s poster.

Because information for each episode is one entry in an array, changing the array index number changes which episode’s data you access. If X = 1, it will grab the thumbnail for the first episode in the JSON data. If it’s 2, it will grab the thumbnail for the second episode, and so on. By using a KLWP global variable here, I refer back to one single variable that decides which episode the wget grabs information from. Each episode shown in my widget is a copy of a single Komponent (KLWP’s grouping system for creating internal “widgets”), where the only thing that’s changed is the global variable, which is increased by 1 for each Komponent copy. That means that for all parts of the Komponent — show title, episode title, episode number, thumb — I just refer to the global variable, copy the component, increase the variable by 1 for each copy, and voila, I have an episode list.

This is the most basic way to use the JSON data, simply referring to a specific object and using it directly in the KLWP configuration for an element. You can however also use the data in more complex ways, such as If statements.

More advanced formulas using JSON data

One example of this can be found in the date indicator in my Trakt widget, which shows “Today” instead of the date if the date is today. Before I continue, I should warn you that I’m not an expert at writing KLWP formulas, so there may be an easier way to do this than what I’ve done. With that in mind, here’s my KLWP formula for the date text, made as human-readable as possible:

wg(“file:///storage/emulated/0/Tasker/trakt.json”, json, “.episodes[“ + gv(number)+ “].day”) = df(dd)
wg(“file:///storage/emulated/0/Tasker/trakt.json”, json, “.episodes[“ + gv(number)+ “].month”) = df(MM)
,df(MMM,a + (wg(“file:///storage/emulated/0/Tasker/trakt.json”, json,”.episodes[“ + gv(number)+ “].month”)-df(MM)) + M)
“ + wg(“file:///storage/emulated/0/Tasker/trakt.json”, json, “.episodes[“ + gv(number)+ “].day”)

This If statement checks if the date for an episode entry is today. If you read my Tasker JSON article, you know that one of the things I do to get the Trakt data the way I want is to split the date up into individual objects for day, month, year, and this is why.

In English, this formula basically says the following: If the day and month in the JSON matches the current day and month, have it say “Today”. If it doesn’t, display the date and month, formatted to show the month as a three letter abbreviation with the date on the next line. df() is a date format formula, which is what turns 04 in the JSON into Apr in the widget. It does this by subtracting the current month from the episode data’s month value, and then using the df formulas add operator to add the difference to today’s month. That means that if the episode airs this month, it adds 0, and shows this month. If it airs next month, it adds 1, and shows next month. As far as I can tell, the alternative would be to specify the full date, which would require three JSON references instead of one — but again, I’m not a KLWP expert, so I may be completely off the trail here.

Final words

Formulas are the backbone of KLWP, so you can do a lot of things with your JSON data. The thumbnails in my widget are for instance in grayscale in the screenshot above, but will show in color based on a criteria I won’t go into detail about here. Other possible criteria for colorization could be if the show airs today, if you’ve seen it (assuming you could grab that data somehow), whether it’s the premiere episode of something, etc. The point I’m trying to make is that you can include so much data so easily in JSON, and then use it to control so many aspects of KLWP.

Your JSON can come from many sources, and since KLWP can also grab RSS and other data sources this way, going the way of Tasker may not be necessary. Heck, there are data processing features in KLWP as well, so if you know how to use it properly, Tasker may be completely redundant. I know Tasker better than I know KLWP, though, and I’m not a huge fan of the KLWP formula editor. For the Trakt widget, I simply gave up trying to get KLWP to grab the fields in the original Trakt RSS XML that used namespaces, instead turning the RSS into JSON in Tasker, doing some other work on the data in the process.

In my opinion, it’s best to have ready-to-use data by the time KLWP needs to deal with it. JSON is a great way to do that, as long as you have a source for it or a way to generate it. It opens up for so many new, custom creations in KLWP, and for Tasker users it provides an alternative to using the plugin.

Pocketables does not accept targeted advertising, phony guest posts, paid reviews, etc. Help us keep this way with support on Patreon!
Become a patron at Patreon!

Andreas Ødegård

Andreas Ødegård is more interested in aftermarket (and user created) software and hardware than chasing the latest gadgets. His day job as a teacher keeps him interested in education tech and takes up most of his time.

Avatar of Andreas Ødegård