Liberty BASIC Community Forum
« parsing .json data from wunderground »

Welcome Guest. Please Login or Register.
Jun 25th, 2017, 05:23am


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: parsing .json data from wunderground  (Read 379 times)
cogburn
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 108
xx parsing .json data from wunderground
« Thread started on: Jun 17th, 2016, 11:12am »

I am able to download via API call from weather underground a couple of pages of weather conditions and forecast data. I do not need all that information but only a small fraction of that so that I may display it on screen. Are there any specific commands for parsing that .json data?
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5314
xx Re: parsing .json data from wunderground
« Reply #1 on: Jun 17th, 2016, 3:45pm »

Does this help? http://lbpe.wikispaces.com/ParsingText
User IP Logged

cundo
Guru
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 620
xx Re: parsing .json data from wunderground
« Reply #2 on: Jun 17th, 2016, 8:27pm »

on Jun 17th, 2016, 11:12am, cogburn wrote:
I am able to download via API call from weather underground a couple of pages of weather conditions and forecast data. I do not need all that information but only a small fraction of that so that I may display it on screen. Are there any specific commands for parsing that .json data?
Do you have an example data? I don't remember seeing a json parser, in fact, I had to do a web search to know what it was.
User IP Logged

cundo
cogburn
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 108
xx Re: parsing .json data from wunderground
« Reply #3 on: Jun 18th, 2016, 3:02pm »

Right now I am using Python to parse and display the data but I am not comfortable with that language. Of course I am not really comfortable with LB but I have done some programming with it. I am much more used to LB so I would like to use LB if possible. Here is my Python code:
Code:

import urllib2
import json
f = urllib2.urlopen('http://api.wunderground.com/api/ebf89bacbc066bec/geolookup/conditions/q/AR/Haskell.json')

json_string = f.read()

parsed_json = json.loads(json_string)

location = parsed_json['location']['city']
temp_f = parsed_json['current_observation']['temp_f']

wind_string = parsed_json['current_observation']['wind_string']
#wind_mph = parsed_json['current_observation']['wind_mph']
#wind_dir = parsed_json['current_observation']['wind_dir']
feelslike_f = parsed_json['current_observation']['feelslike_f']

relative_humidity = parsed_json['current_observation']['relative_humidity']
precip_today_in = parsed_json['current_observation']['precip_today_in']
pressure_in = parsed_json['current_observation']['pressure_in']




print "HASKELL WEATHER STATION"
print " -92.58484650 degrees longitude :  34.56635666 degrees latitude : 295 feet elevation"
print ""
print "Current temperature in %s is: %s" % (location, temp_f),"F","  ***  Feels Like %s" % (feelslike_f), "F"
#print "Feels Like %s" % (feelslike_f), "degrees F"                                                 
print "Atmospheric pressure is: %s" % (pressure_in),"in Hg"
#print "Wind speed is: %s" % (wind_mph),"mph","out of the %s" % (wind_dir)
print "Wind is: %s" % (wind_string)
print "Relative Humidity is: %s" % (relative_humidity)
print "Rainfall Today was: %s" % (precip_today_in),"inches"

f.close()


 


The following is an example of the raw data I get from wunderground when I run the API.

{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"conditions": 1
}
}
, "current_observation": {
"image": {
"url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
"title":"Weather Underground",
"link":"http://www.wunderground.com"
},
"display_location": {
"full":"Sheridan, AR",
"city":"Sheridan",
"state":"AR",
"state_name":"Arkansas",
"country":"US",
"country_iso3166":"US",
"zip":"72150",
"magic":"1",
"wmo":"99999",
"latitude":"34.30760956",
"longitude":"-92.40081787",
"elevation":"86.00000000"
},
"observation_location": {
"full":"K5BTM - Sheridan, Sheridan, Arkansas",
"city":"K5BTM - Sheridan, Sheridan",
"state":"Arkansas",
"country":"US",
"country_iso3166":"US",
"latitude":"34.264088",
"longitude":"-92.391708",
"elevation":"295 ft"
},
"estimated": {
},
"station_id":"KARSHERI2",
"observation_time":"Last Updated on June 18, 2:54 PM CDT",
"observation_time_rfc822":"Sat, 18 Jun 2016 14:54:46 -0500",
"observation_epoch":"1466279686",
"local_time_rfc822":"Sat, 18 Jun 2016 15:01:19 -0500",
"local_epoch":"1466280079",
"local_tz_short":"CDT",
"local_tz_long":"America/Chicago",
"local_tz_offset":"-0500",
"weather":"Partly Cloudy",
"temperature_string":"95.2 F (35.1 C)",
"temp_f":95.2,
"temp_c":35.1,
"relative_humidity":"47%",
"wind_string":"From the NW at 2.2 MPH",
"wind_dir":"NW",
"wind_degrees":315,
"wind_mph":2.2,
"wind_gust_mph":0,
"wind_kph":3.5,
"wind_gust_kph":0,
"pressure_mb":"1018",
"pressure_in":"30.06",
"pressure_trend":"+",
"dewpoint_string":"72 F (22 C)",
"dewpoint_f":72,
"dewpoint_c":22,
"heat_index_string":"104 F (40 C)",
"heat_index_f":104,
"heat_index_c":40,
"windchill_string":"NA",
"windchill_f":"NA",
"windchill_c":"NA",
"feelslike_string":"104 F (40 C)",
"feelslike_f":"104",
"feelslike_c":"40",
"visibility_mi":"10.0",
"visibility_km":"16.1",
"solarradiation":"--",
"UV":"8","precip_1hr_string":"-999.00 in ( 0 mm)",
"precip_1hr_in":"-999.00",
"precip_1hr_metric":" 0",
"precip_today_string":"0.00 in (0 mm)",
"precip_today_in":"0.00",
"precip_today_metric":"0",
"icon":"partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
"forecast_url":"http://www.wunderground.com/US/AR/Sheridan.html",
"history_url":"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KARSHERI2",
"ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=34.264088,-92.391708",
"nowcast":""
}
}
User IP Logged

meerkat
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 39
xx Re: parsing .json data from wunderground
« Reply #4 on: Jun 19th, 2016, 07:54am »

I parse data from the web quite often.
I find that replacing the html tags with something helps. For example if you read the html file into a$ then b$ = tags$(a$,"|") gets rid of all the tags and replaces them with "|". Just a lot easier to look at the data without all the confusing html <tags>. Then using searches and word$() you can pull your data apart.
The tags$() function
Code:
' --------------------------------
' Replace HTML <tags> with$
' --------------------------------
FUNCTION tags$(tag$,with$)
i	= instr(tag$,"<")
j	= instr(tag$,">",i)
WHILE j > i
	ln	= (j - i) + 1
	x$	= mid$(tag$,i,ln)
	tag$	= replace$(tag$,x$,with$)
	i	= instr(tag$,"<")
	j	= instr(tag$,">",i)
WEND
tags$	= tag$
END FUNCTION
' --------------------------------
' string replace rep str with
' --------------------------------
FUNCTION replace$(str$,rep$,with$)
ln  = len(rep$)
ln1 = ln - 1
i = 1
while i <= len(str$)
    if mid$(str$,i,ln) = rep$ then
        replace$ = replace$ + with$
        i = i + ln1
    else
        replace$ = replace$ + mid$(str$,i,1)
    end if
i = i + 1
WEND
END FUNCTION
 

Hope this helps..
« Last Edit: Jun 19th, 2016, 08:00am by meerkat » User IP Logged

tenochtitlanuk
Board Moderator

member is offline

Avatar




Homepage PM

Gender: Male
Posts: 1154
xx Re: parsing .json data from wunderground
« Reply #5 on: Jun 19th, 2016, 5:10pm »

I saved your example as 'parse.json' and ran the following code. Works for me...
Code:
global j$

open "parse.json" for input as #fi
    j$ =input$( #fi, lof( #fi))
close #fi

print "       LB WEATHER STATION"
print " -92.58484650 degrees longitude"
print "  34.56635666 degrees latitude"
print "       295 feet elevation"
print ""
print " Current temperature is: "; val( json$( "temp_f")); "F & feels like "; jsonQ$( "feelslike_f"); "F."
print " Atmospheric pressure is: "; val( jsonQ$( "pressure_in")); " inches of Hg."
print " Wind speed is: "; val( json$( "wind_mph")); " mph, out of the "; jsonQ$( "wind_dir")
print " Wind is: "; jsonQ$( "wind_string")
print " Relative Humidity is: "; jsonQ$( "relative_humidity")
print " Rainfall Today was: "; jsonQ$( "precip_today_in"); " inches."

end

function json$( find$)  '   finds the data string immediately after this section-header
    startPtr =instr( j$, find$) +2 +len( find$)
    endPtr   =instr( j$, ",", startPtr)
    lenData  =endPtr -startPtr
    json$    =mid$( j$, startPtr, lenData)
end function

function jsonQ$( find$) '   finds the data string , but omitting leading/trailing quotes
    startPtr =instr( j$, find$) +2 +len( find$)
    endPtr   =instr( j$, ",", startPtr)
    lenData  =endPtr -startPtr
    jsonQ$    =mid$( j$, startPtr +1, lenData -2)
end function
 


Result..
Code:
       LB WEATHER STATION
 -92.58484650 degrees longitude
  34.56635666 degrees latitude
       295 feet elevation

 Current temperature is: 95.2F & feels like 104F.
 Atmospheric pressure is: 30.06 inches of Hg.
 Wind speed is: 2.2 mph, out of the NW
 Wind is: From the NW at 2.2 MPH
 Relative Humidity is: 47%
 Rainfall Today was: 0.00 inches.
 
« Last Edit: Jun 19th, 2016, 5:10pm by tenochtitlanuk » User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls