Pulling multiple parameters from a single JSON result
-
Hey hey, I was wondering if doing something like the following would be possible.
The DarkSky API allows me to get a JSON stream of, for example, the hourly temperatures from yesterday. I've figured out a regex which allows me to find each individual temperature as a group. What I'm wondering is is there a way I can pull each one of those temperature readings in so I can average them all once a day? I was thinking maybe into an alphanumeric point and them via scripting average them?
I have a solution which I think would work, I can just pull weather data once an hour into a "current temperature" point and then average that at the end of the day, so no rush. I just think being able to do it using one query would be way cooler (although no one will appreciate it :))
-
Hi Psysak,
Both those should work. If you alphanumeric matches the whole response you can do something like
var response = JSON.parse(alphanum.value);
and then you can process it however you like. Then trigger the script on the context update of the alphanumeric point. -
Hi Psysak,
Also, you may consider using the HttpBuilder script utility. Then you can have the whole thing in a scripting data source, clean and tidy.
There is documentation in the contextual help menu for "Mango Java Script" (which you can access through the scripting help, or the meta point help).
Here's a thread where I posted something maybe more complex than what you're talking about: https://forum.infiniteautomation.com/topic/2999/intesis-integration
It sounds like yours may be as simple as...
var headers = {} var parameters = {} function handleTemperatureData(data) { /* Do stuff */ } HttpBuilder.get("https://someplace.wherever", headers, parameters).err(function(status, headers, content) { throw "Script failed with HTTP status: " + status; }).resp(function(status, headers, content) { //needs to be 200 status or you can set the accepted statuses handleTemperatureData( JSON.parse(content) ); }).execute();
-
Thanks Phil, just trying to get back at this finally.
-
OMG man, I didn't realize what JSON.parse() did.... now I do... DAMN! :)
-
Very powerful! I've definitely done many quick things by serializing data into an alphanumeric point with JSON.stringify(object) and getting it back elsewhere with JSON.parse(string)