You can doing amazing things with the tools that are available to you, if you only take the time to learn how to use them. That was how I felt after taking the time to dig into the details of JSONPath. The Kynetx KRL language contains a number of mini languages, regex, jQuery, JSONPath, etc. While I was fortunate to have JSON data source for the CloudStatus application, the structure of the data was not usable. Or so I initially thought.

First some background on the CloudStatus application. The CloudStatus application will provide a list of public API services that are having performance issues. Basically it will tell you which services are not ok. Fortunately I was able to find the JSON (actually JSONP) data which is used to build Public API Status. However, the JSON was structured so that it could be easily used to build a website which reports the status of all 26 services, not just report the bad ones. No problem I'll just write another utility application to transform the JSON into a structure that will be easier for the CloudStatus application to consume.

Good idea, right? Well, yes, no, maybe. Honestly I really did not want to spend the time to code, test and debug an application that transformed JSON data. Sorry, it's just too tedious! But being lazy in this regard motivated me to dig into the details of what can be done with JSONPath. It's far more powerful that I initially assumed. And after about 30 minutes I was able to craft a JSONPath expression that extracted the data that I needed. Awesome!

The source JSON data was structured with the list of 26 services in random order. With 6 attributes provided for each of the services, including the name of the service and current status (ok, warn, or err). All that we need for the CloudStatus app is the list of services that are not ok. Using a JSONPath filter expression we grab a list of services where current status is not ok (current_status ne 'ok'). But since we only need the name of the services, not all of the attributes, we'll just retrieve the name:

BadServices = status_json.pick("$[?(@.current_status ne 'ok')].name")

Now BadServices contains array of the services names that are having performance issues. And we didn't have to hack a pile of tedious code to transform the JSON into a new structure for our application to consume. Three cheers for JSONPath!

["bing search API", " API", "Flickr API"]

We still need to get the list into string format so that it can be passed to Twilio, and from an end user point of view we should remove the text "API". Using the KRL join operator we can convert the array to a string:

BadString = BadServices.join(" and ")

Giving us the the BadString:

"bing search API and API and Flickr API"

Finally, we remove all occurrences of the string "API" using the replace operator.

YouAreBad = BadString.replace(re/ API/g, "")

Which gives us a string containing the list of bad services:

"bing search and and Flickr"

Enjoy and happy hack'in!