View Single Post
 
Old 12-18-2018, 07:26 PM
Kenneth Hobson Kenneth Hobson is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: Jun 2018
Posts: 37
Kenneth Hobson is on a distinguished road
Default

Install the parsing routine as I commented. Add the library as commented since the parsing routine needs it. Pass your own key value.

Code:
Sub Test_QuickGetZipCodeDetails()
  'Register to get key: https://www.zip-codes.com/zip-code-api-register.asp
  '250 free queries per month limit.
  MsgBox Join(QuickGetZipCodeDetails(90210, "123456789abcdefghij"), vbLf)
End Sub


'Get latest jsonconverter.bas, https://github.com/VBA-tools/VBA-JSON/releases
'The routine above needs library: Tools > References > Microsoft Scripting Runtime
'Similar to: https://codingislove.com/excel-json/
Function QuickGetZipCodeDetails(zip, key$)
  Dim http As Object, JSON As Object, a(1 To 6), s$
  
  Set http = CreateObject("MSXML2.XMLHTTP")
  http.Open "GET", "https://api.zip-codes.com/ZipCodesAPI.svc/1.0/QuickGetZipCodeDetails/" & _
    zip & "?key=" & key, False
  http.Send
  s = http.responseText
  Set JSON = ParseJson(s)
  
  a(1) = JSON("City")
  a(2) = JSON("State")
  a(3) = JSON("Latitude")
  a(4) = JSON("Longitude")
  a(5) = JSON("ZipCode")
  a(6) = JSON("County")
  
  QuickGetZipCodeDetails = a
End Function
Reply With Quote