How can I print a table, converted from json

Post Reply
JanBlomqvist
Posts: 2
Joined: Monday 16 October 2017 9:18
Target OS: Raspberry Pi
Domoticz version:
Contact:

How can I print a table, converted from json

Post by JanBlomqvist » Friday 16 February 2018 11:52

How can I print a table, converted from json so that I understand how to access different parts.
I have an json from wunderground, where I want to get the weather text report. I use dzVents.

http://api.wunderground.com/api/<API-KE ... HO432.json
This is what I see when I log the domoticz.json in the HTTPResponse:

{["forecast"]={["txt_forecast"]={["date"]="8:33 AM CET", ["forecastday"]={{["fcttext"]="Övervägande molnigt. Högsta 35ºF. Vindar SSÖ på 10 till 15 mph.", ["title"]="Torsdag", ["icon"]="mostlycloudy", ["pop"]="10", ["fcttext_metric"]="Övervägande molnigt. Högsta 1ºC. Vindar SSÖ på 10 till 15 km/tim.", ["icon_url"]="http://icons.wxug.com/i/c/k/mostlycloudy.gif", ["period"]=0}, {["fcttext"]="Molnigt. Lägsta 29ºF. Vindar SÖ på 5 till 10 mph.", ["title"]="Torsdag natt", ["icon"]="nt_cloudy", ["pop"]="20", ["fcttext_metric"]="Molnigt. Lägsta -2ºC. Vindar SÖ på 10 till 15 km/tim.", ["icon_url"]="http://icons.wxug.com/i/c/k/nt_cloudy.gif", ["period"]=1}, {["fcttext"]="Snöbyar under eftermiddagen. Högsta 36ºF. Vindar SÖ på 5 till 10 mph. Sannolikhet snö 40%.", ["title"]="Fredag", ["icon"]="snow", ["pop"]="40", ["fcttext_metric"]="Snöbyar under eftermiddagen. Högsta 2ºC. Vindar SÖ på 10 till 15 km/tim. Sannolikhet snö 40%.", ["icon_url"]="http://icons.wxug.com/i/c/k/snow.gif", ["period"]=2}, {["fcttext"]="Snöbyar tidigt. Lägsta 28ºF. Vindar NÖ och växlande. Sannolikhet snö 40%.", ["title"]="Fredag natt", ["icon"]="nt_chancesnow", ["pop"]="40", ["fcttext_metric"]="Snöbyar tidigt. Lägsta -2ºC. Vindar NÖ och växlande. Sannolikhet snö 50%.", ["icon_url"]="http://icons.wxug.com/i/c/k/nt_chancesnow.gif", ["period"]=3}, {["fcttext"]="Moln under morgonen följt av eftermiddagssol. Högsta 33ºF. Vindar NÖ och växlande.", ["title"]="Lördag", ["icon"]="partlycloudy", ["pop"]="10", ["fcttext_metric"]="Moln under morgonen följt av eftermiddagssol. Högsta 1ºC. Vindar NÖ och växlande.", ["icon_url"]="http://icons.wxug.com/i/c/k/partlycloudy.gif", ["period"]=4}, {["fcttext"]="Halvklart. Köld förväntas. Lägsta 22ºF. Vindar SSV och växlande.", .. and so on.

I would like a script function that could print something like this (accessor=value)
forecast.forecastday.forecastday.fcttext.metric = "Snöbyar ...
so it is easy to understand how to get data from large cumbersome json files.

Secondly, if someone could help me how I can access the parts in bold.

Thanks/Jan

elmortero
Posts: 189
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 2.3852
Location: Spain
Contact:

Re: How can I print a table, converted from json

Post by elmortero » Friday 16 February 2018 14:04

Copy and paste the response in Notepad++ and select as language JSon (or install the json viewer plugin in chrome) and you will get exactly what you are asking here.

User avatar
McMelloW
Posts: 193
Joined: Monday 20 November 2017 18:01
Target OS: Raspberry Pi
Domoticz version: V3.beta
Location: Harderwijk, NL
Contact:

Re: How can I print a table, converted from json

Post by McMelloW » Friday 16 February 2018 14:25

Copy and paste to https://jsonlint.com/ and click on Validate JSON
Greetings McMelloW

User avatar
Egregius
Posts: 2662
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: How can I print a table, converted from json

Post by Egregius » Thursday 19 April 2018 8:31

JanBlomqvist wrote:
Friday 16 February 2018 11:52
I use dzVents.
Then why post in the PHP board and not in the dzvents board?
In PHP that would be peace of cake:

Code: Select all

$wu=@json_decode(@file_get_contents('http://api.wunderground.com/api/a123456789b/conditions/q/BX/Beitem.json'),true);
if(isset($wu['current_observation'])){
	//Check when we last had a good response
	$lastobservation=apcu_fetch('wu-observation');
	//Check if the response is a good one or a estimated, if estimated exit.
	if(isset($wu['current_observation']['estimated']['estimated']))goto exitwunderground;
	//If timestamp of observation is older than our last successful exit
	elseif($wu['current_observation']['observation_epoch']<=$lastobservation)goto exitwunderground;
	//Store needed values in vars for later use in script.
	if(isset($wu['current_observation']['temp_c'])){$wutemp=$wu['current_observation']['temp_c'];if($wutemp>$prevtemp+0.5)$wutemp=$prevtemp+0.5;elseif($wutemp<$prevtemp-0.5)$wutemp=$prevtemp-0.5;}
	if(isset($wu['current_observation']['wind_kph']))$wuwind=$wu['current_observation']['wind_kph'];
	if(isset($wu['current_observation']['wind_gust_kph']))if($wu['current_observation']['wind_gust_kph']>$wuwind)$wuwind=$wu['current_observation']['wind_gust_kph'];
	if(isset($wu['current_observation']['precip_1hr_metric']))$wubuien=$wu['current_observation']['precip_1hr_metric']*35;
	if(isset($wu['current_observation']['wind_dir']))apcu_store('winddir',$wu['current_observation']['wind_dir']);
	if(isset($wu['current_observation']['icon']))apcu_store('icon',$wu['current_observation']['icon']);
	//Store the timestamp of this observation
	apcu_store('wu-observation',$wu['current_observation']['observation_epoch']);
}
exitwunderground:

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests