This page last changed on May 21, 2016 by robnas.

Hello,

I would like to parse the temperature from a json command.
he urls is: http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22roermond%2C%20nl%22)%20and%20u=%27c%27&format=json

the json string is:

{
	"query": {
		"count": 1,
		"created": "2016-05-21T18:26:40Z",
		"lang": "nl-NL",
		"results": {
			"channel": {
				"units": {
					"distance": "km",
					"pressure": "mb",
					"speed": "km/h",
					"temperature": "C"
				},
				"title": "Yahoo! Weather - Roermond, LM, NL",
				"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-733020/",
				"description": "Yahoo! Weather for Roermond, LM, NL",
				"language": "en-us",
				"lastBuildDate": "Sat, 21 May 2016 08:26 PM CEST",
				"ttl": "60",
				"location": {
					"city": "Roermond",
					"country": "Netherlands",
					"region": " LM"
				},
				"wind": {
					"chill": "75",
					"direction": "175",
					"speed": "22.53"
				},
				"atmosphere": {
					"humidity": "43",
					"pressure": "34134.81",
					"rising": "0",
					"visibility": "25.91"
				},
				"astronomy": {
					"sunrise": "5:35 am",
					"sunset": "9:31 pm"
				},
				"image": {
					"title": "Yahoo! Weather",
					"width": "142",
					"height": "18",
					"link": "http://weather.yahoo.com",
					"url": "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"
				},
				"item": {
					"title": "Conditions for Roermond, LM, NL at 07:00 PM CEST",
					"lat": "51.18988",
					"long": "5.9911",
					"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-733020/",
					"pubDate": "Sat, 21 May 2016 07:00 PM CEST",
					"condition": {
						"code": "26",
						"date": "Sat, 21 May 2016 07:00 PM CEST",
						"temp": "23",
						"text": "Cloudy"
					},
					"forecast": [{
						"code": "26",
						"date": "21 May 2016",
						"day": "Sat",
						"high": "24",
						"low": "11",
						"text": "Cloudy"
					}, {
						"code": "11",
						"date": "22 May 2016",
						"day": "Sun",
						"high": "20",
						"low": "13",
						"text": "Showers"
					}, {
						"code": "11",
						"date": "23 May 2016",
						"day": "Mon",
						"high": "16",
						"low": "11",
						"text": "Showers"
					}, {
						"code": "28",
						"date": "24 May 2016",
						"day": "Tue",
						"high": "16",
						"low": "8",
						"text": "Mostly Cloudy"
					}, {
						"code": "28",
						"date": "25 May 2016",
						"day": "Wed",
						"high": "20",
						"low": "7",
						"text": "Mostly Cloudy"
					}, {
						"code": "30",
						"date": "26 May 2016",
						"day": "Thu",
						"high": "24",
						"low": "11",
						"text": "Partly Cloudy"
					}, {
						"code": "4",
						"date": "27 May 2016",
						"day": "Fri",
						"high": "25",
						"low": "13",
						"text": "Thunderstorms"
					}, {
						"code": "47",
						"date": "28 May 2016",
						"day": "Sat",
						"high": "23",
						"low": "13",
						"text": "Scattered Thunderstorms"
					}, {
						"code": "28",
						"date": "29 May 2016",
						"day": "Sun",
						"high": "22",
						"low": "15",
						"text": "Mostly Cloudy"
					}, {
						"code": "28",
						"date": "30 May 2016",
						"day": "Mon",
						"high": "21",
						"low": "13",
						"text": "Mostly Cloudy"
					}],
					"description": "<![CDATA[<img src=\"http://l.yimg.com/a/i/us/we/52/26.gif\"/>\n<BR />\n<b>Current Conditions:</b>\n<BR />Cloudy\n<BR />\n<BR />\n<b>Forecast:</b>\n<BR /> Sat - Cloudy. High: 24Low: 11\n<BR /> Sun - Showers. High: 20Low: 13\n<BR /> Mon - Showers. High: 16Low: 11\n<BR /> Tue - Mostly Cloudy. High: 16Low: 8\n<BR /> Wed - Mostly Cloudy. High: 20Low: 7\n<BR />\n<BR />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-733020/\">Full Forecast at Yahoo! Weather</a>\n<BR />\n<BR />\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)\n<BR />\n]]>",
					"guid": {
						"isPermaLink": "false"
					}
				}
			}
		}
	}
}

the json expression should be:

$.query.results.channel.item.condition.temp

its tested and this works according to http://jsonpath.com/
it will get the temperature back in degrees

my http.log says:

ERROR 2016-05-21 22:49:54,598 (HTTP): ClientProtocolException when executing HTTP method
org.apache.http.client.HttpResponseException: Bad Request
	at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:67)
	at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:54)
	at org.openremote.controller.protocol.http.HttpGetCommand.requestURL(HttpGetCommand.java:238)
	at org.openremote.controller.protocol.http.HttpGetCommand.run(HttpGetCommand.java:260)
	at java.lang.Thread.run(Thread.java:662)
INFO 2016-05-21 22:49:54,602 (HTTP): received message: 
ERROR 2016-05-21 22:49:55,428 (HTTP): Could not perform jsonpath evaluation
java.lang.IllegalArgumentException: json can not be null or empty
	at org.apache.commons.lang.Validate.notEmpty(Validate.java:363)
	at com.jayway.jsonpath.JsonPath.read(JsonPath.java:304)
	at org.openremote.controller.protocol.http.HttpGetCommand.run(HttpGetCommand.java:297)
	at java.lang.Thread.run(Thread.java:662)
ERROR 2016-05-21 22:50:33,972 (HTTP): IOException when executing HTTP method
java.net.NoRouteToHostException: No route to host
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
	at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
	at java.net.Socket.connect(Socket.java:529)
	at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:123)
	at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:123)
	at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:147)
	at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:108)
	at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415)
	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
	at org.openremote.controller.protocol.http.HttpGetCommand.requestURL(HttpGetCommand.java:237)
	at org.openremote.controller.protocol.http.HttpGetCommand.run(HttpGetCommand.java:260)
	at java.lang.Thread.run(Thread.java:662)
INFO 2016-05-21 22:50:33,974 (HTTP): received message: 
ERROR 2016-05-21 22:50:36,926 (HTTP): Could not perform jsonpath evaluation
java.lang.IllegalArgumentException: json can not be null or empty
	at org.apache.commons.lang.Validate.notEmpty(Validate.java:363)
	at com.jayway.jsonpath.JsonPath.read(JsonPath.java:304)
	at org.openremote.controller.protocol.http.HttpGetCommand.run(HttpGetCommand.java:297)
	at java.lang.Thread.run(Thread.java:662)
ERROR 2016-05-21 22:50:45,677 (HTTP): ClientProtocolException when executing HTTP method
org.apache.http.client.HttpResponseException: Bad Request
	at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:67)
	at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:54)
	at org.openremote.controller.protocol.http.HttpGetCommand.requestURL(HttpGetCommand.java:238)
	at org.openremote.controller.protocol.http.HttpGetCommand.run(HttpGetCommand.java:260)
	at java.lang.Thread.run(Thread.java:662)
INFO 2016-05-21 22:50:45,679 (HTTP): received message: 
ERROR 2016-05-21 22:50:51,329 (HTTP): Could not perform jsonpath evaluation
java.lang.IllegalArgumentException: json can not be null or empty
	at org.apache.commons.lang.Validate.notEmpty(Validate.java:363)
	at com.jayway.jsonpath.JsonPath.read(JsonPath.java:304)
	at org.openremote.controller.protocol.http.HttpGetCommand.run(HttpGetCommand.java:297)
	at java.lang.Thread.run(Thread.java:662)

any ideas what should be wrong?

Ive got it working already with http://openweathermap.org/ instead of using Yahoo. Yahoo doesnt worksomehow.

Posted by robnas at May 22, 2016 14:04
Document generated by Confluence on Jun 05, 2016 09:33