WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

JSON
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

GeoNames WSC Json Sample

 Keywords:  GeoNames WSC JSON 

You may be interested in parsing JSON. The following script and .wsc file processes JSON as returned from a webservice. The trend seems to be moving from XML to JSON for size and therefore speed concerns. The .wsc file has a jscript function specific to the output. It is a good source of postalcode information.

The script has username=WBdemo as part of the url. It was a demo user account created on the site. Note you might get an alert message [the script traps for it] saying the credits are used up. IF this is the case try creating your own account. You will need to log in and spcify a managed my account for the free web services.

IMPORTANT: The .wsc is specific to the JSON format, and that is for US postalcodes.

;Winbatch 2001A - Simple JSON WebService
;
;
;see http://www.geonames.org/export/ajax-postalcode-autocomplete.html  for a reference
;goto main site to set up your own login
;
;Available Country Codes
;AT = Austria
;AU = Australia
;BE = Belgium
;CH = Switzerland
;CZ = Czech Republic
;DE = Germany
;DK = Denmark
;ES = Spain
;FI = Finland
;FR = France
;IN = India
;IS = Iceland
;IT = Italy
;HR = Croatia
;HU = Hungary
;LI = Liechtenstein
;LU = Luxembourg
;NL = The Netherlands
;NO = Norway
;NZ = New Zealand
;MX = Mexico
;PK = Pakistan
;PT = Portugal
;PL = Poland
;SI = Slovenia
;SK = Slovakia
;SM = San Marino
;LK = Sri Lanka
;TH = Thailand
;US = United States
;VA = Vatican
;ZA = South Africa
;
;Example is from my zipcode, feel free to try something different
;
;Stan Littlefield, August 30 2011
;///////////////////////////////////////////////////////////////////////////////////////////////////////////////

;create outout file for inspecting/optional comment if not needed
cFile=DirScript():"jtxt.txt"

;create jscript wsc to handle json parsing
;contains a simple call to eval() to return a json object
cWSC = StrCat(DirScript(),"jsonparse.wsc")
If ! FileExist(cWSC) Then Terminate(@TRUE,"Sorry...",cWSC:" not located.")
oWSC = StrCat("script:",cWSC)
oWSC = GetObject(oWSC)


;send URL as a request

cZip="27603"
cCountry="US"

;NOTE:username=demo they allow a certain number of hits per day under the username
;     you can login and use their free web service then substitute your username for demo
cURL="http://api.geonames.org/postalCodeLookupJSON?postalcode=%cZip%&country=%cCountry%&username=WBdemo"
oHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
oHTTP.Open("GET", cURL, @FALSE)
oHTTP.Send()
oHTTP.WaitForResponse()
jsondata=oHTTP.ResponseText ;data is returned as JSON - now parse it

;optional - just see the json data
;if you are using demo as a user, it often fills up its credits and
;returns an exception
;Pause('Results',jsondata)

If StrIndex(jsondata,"placeName",0,@FWDSCAN) ;try to avoid invalid JSON data received
                                             ;i.e. alerts or other error messages
   cJson= oWSC.getit(jsondata) ;returns data as a parsed string
   Message("",cJson)
   ;optional - write parsed data to file
   FilePut(cFile,cJson)
Else
   Pause('Error or Message',jsondata)
EndIf

:End
oWSC=0

Exit

;////////////////////////////////////////////////////////////////////////////////////////////////////////////////


jsonparse.wsc

<?xml Version="1.0"?>
<component>

<?component error="true" Debug="true"?>

<registration
   description="jsonparse"
   progid="jsonparse.WSC"
   Version="1.00"
   classid="{99a11205-0e27-4089-8791-81c9adf50a24}"
>
</registration>

<public>
   <method name="getit">
      <PARAMETER name="jdata"/>
   </method>
</public>

<implements type="Behavior" id="Behavior"/>

<script language="JScript">
<![CDATA[

var description = new jsonparse;

function jsonparse()
{

   this.getit = getit;
}

function getit(jdata)
{
   var J= eval('(' + jdata + ')');
   var S = "";
   S=S.concat("ZipCode=",J.postalcodes[0].postalcode,"\n");
   S=S.concat("County=",J.postalcodes[0].adminName2,"\n");
   S=S.concat("City=",J.postalcodes[0].placeName,"\n");
   S=S.concat("State=",J.postalcodes[0].adminName1,"\n");
   S=S.concat("County=",J.postalcodes[0].adminName2,"\n");
   S=S.concat("latitude=",J.postalcodes[0].lat,"\n");
   S=S.concat("longitude=",J.postalcodes[0].lng,"\n");
   S=S.concat("Country=",J.postalcodes[0].countryCode,"\n");
   Return S;
}

]]>
</script>

</component>

Article ID:   W17961
Filename:   GeoNames WSC Json Sample.txt
File Created: 2012:04:19:08:28:20
Last Updated: 2012:04:19:08:28:20