Help with parsing json data from web

Hi guys,

Sorry but I’m a complete noob and have tried a lot with trial and error to get the wanted forecast information from following json return

{“city”:{“city_code”:“ATAT10549”,“name”:“Schwechat”,“url”:“oesterreich/schwechat/ATAT10549.html”,“credit”:{“info”:“In order to use the free weather data from wetter.com you HAVE TO display at least two out of three of the following possibilities: text, link, logo”,“text”:“Powered by wetter.com”,“link”:“http://www.wetter.com”,“logo”:“Download at http://www.wetter.com/api/downloads/#logos"},“forecast”:{“2016-03-07”:{“w”:“61”,“tx”:“6”,“pc”:“90”,“06:00”:{“w”:“61”,“tx”:“5”,“pc”:“90”,“tn”:“4”,“p”:“5”,“d”:“1457330400”,“w_txt”:"leichter Regen”},“11:00”:{“w”:“61”,“tx”:“6”,“pc”:“90”,“tn”:“5”,“p”:“6”,“d”:“1457348400”,“w_txt”:“leichter Regen”},“17:00”:{“w”:“61”,“tx”:“5”,“pc”:“90”,“tn”:“3”,“p”:“6”,“d”:“1457370000”,“w_txt”:“leichter Regen”},“23:00”:{“w”:“61”,“tx”:“3”,“pc”:“90”,“tn”:“3”,“p”:“6”,“d”:“1457391600”,“w_txt”:“leichter Regen”},“d”:“1457330400”,“tn”:“3”,“p”:“24”,“w_txt”:“leichter Regen”},“2016-03-08”:{“w”:“61”,“tx”:“6”,“pc”:“90”,“06:00”:{“w”:“61”,“tx”:“5”,“pc”:“90”,“tn”:“2”,“p”:“5”,“d”:“1457416800”,“w_txt”:“leichter Regen”},“11:00”:{“w”:“61”,“tx”:“6”,“pc”:“90”,“tn”:“5”,“p”:“6”,“d”:“1457434800”,“w_txt”:“leichter Regen”},“17:00”:{“w”:“2”,“tx”:“5”,“pc”:“30”,“tn”:“3”,“p”:“6”,“d”:“1457456400”,“w_txt”:“wolkig”},“23:00”:{“w”:“1”,“tx”:“3”,“pc”:“20”,“tn”:“2”,“p”:“6”,“d”:“1457478000”,“w_txt”:“leicht bewölkt”},“d”:“1457416800”,“tn”:“2”,“p”:“24”,“w_txt”:“leichter Regen”},“2016-03-09”:{“w”:“1”,“tx”:“8”,“pc”:“20”,“06:00”:{“w”:“1”,“tx”:“5”,“pc”:“20”,“tn”:“1”,“p”:“5”,“d”:“1457503200”,“w_txt”:“leicht bewölkt”},“11:00”:{“w”:“1”,“tx”:“8”,“pc”:“10”,“tn”:“5”,“p”:“6”,“d”:“1457521200”,“w_txt”:“leicht bewölkt”},“17:00”:{“w”:“1”,“tx”:“8”,“pc”:“10”,“tn”:“4”,“p”:“6”,“d”:“1457542800”,“w_txt”:“leicht bewölkt”},“23:00”:{“w”:“1”,“tx”:“4”,“pc”:“10”,“tn”:“2”,“p”:“6”,“d”:“1457564400”,“w_txt”:“leicht bewölkt”},“d”:“1457503200”,“tn”:“1”,“p”:“24”,“w_txt”:“leicht bewölkt”}}}}

I would like to retrieve the “w” values but as they are not defined as an array and date will change I think I cannot define it as an item ?

String Forecast_Weather7 “Weather Forecast7 [%s]” { http="<[weatherCache:5000:JSONPATH($…w[6])]" }

does not give the correct value as the sequence of the “w” values are mixed up


I have also tried to make it work in a rule without success (with an online parser checker it works fine)

rule "Weather Today and Tomorrow"
when
Time cron “0/10 * * * * ?” or
System started
then
var String Today = now.toString(PATTERN)
var String Tomorrow = now.plusDays(1).toString(PATTERN)
var String json = (Forecast_Weather.state as StringType).toString
var String Trans = “$…” + Today + ".w"
var String test = transform(“JSONPATH”, Trans , json)
Forecast_Test.postUpdate(test)
end

I would appreciate any hint which direction to go :slight_smile:

Kind regards
Chris

I Copied the Data to NP++ and used the JSON Viewer, seems to be well ordered. :slight_smile:

JSONPATH($.city.forecast.2016-03-07.w) should be the w for the 1st complete day
JSONPATH($.city.forecast.2016-03-07.06:00.w) should be the w for the 6 O’ clock forecast and so on, but I don’t know how to get rid of absolute date and hour.

thanks Udo

Solved :slight_smile: … just get the full string with Regex((.*)) and the rest is done in rules.
Maybe not the most elegant way but it works :slight_smile:

String Forecast_Input "Forecast Input [%s]" { http="<[weatherCache:5000:REGEX((.*))]" }

val String PATTERN = ( "YYYY-MM-dd" )
	
rule "Weather Today and Tomorrow"
when
	Time cron "0 0/15 * * * ?" or
   	System started
then
	var String Today = now.toString(PATTERN)
	var String Tomorrow = now.plusDays(1).toString(PATTERN)
	
	var json = Forecast_Input.state.toString
	
	var String Trans_Today = "$.." + Today + ".w" 
	var String Trans_Tomorrow = "$.." + Tomorrow + ".w" 
	
	Forecast_Rain_Today.postUpdate(transform("JSONPATH", Trans_Today , json))
	Forecast_Rain_Tomorrow.postUpdate(transform("JSONPATH", Trans_Tomorrow , json))
end