Geeks of Coding

Join us on Telegram

Viewing 0 reply threads
  • Author
    • #895
      Abhishek TyagiAbhishek Tyagi

      Calling a JSON API

      In this assignment you will write a Python program somewhat similar to The program will prompt for a location, contact a web service and retrieve JSON for the web service and parse that data, and retrieve the first place_id from the JSON. A place ID is a textual identifier that uniquely identifies a place as within Google Maps.
      API End Points

      To complete this assignment, you should use this API endpoint that has a static subset of the Google Data:
      This API uses the same parameter (address) as the Google API. This API also has no rate limit so you can test as often as you like. If you visit the URL with no parameters, you get a list of all of the address values which can be used with this API.
      To call the API, you need to provide address that you are requesting as the address= parameter that is properly URL encoded using the urllib.urlencode() fuction as shown in

      #Enter location: University of Twente
      #Retrieved 2124 characters
      #Place id ChIJPZ9qp0tvv4cRb5oLVI9wra8
      import urllib.request as ur
      import urllib.parse as up
      import json
      serviceurl = ""
      address_input = input("Enter location: ")
      params = {"sensor": "false", "address": address_input}
      url = serviceurl + up.urlencode(params)
      print("Retrieving ", url)
      data = ur.urlopen(url).read().decode('utf-8')
      print('Retrieved', len(data), 'characters')
      json_obj = json.loads(data)
      place_id = json_obj["results"][0]["place_id"]
      print("Place id", place_id)
Viewing 0 reply threads
  • You must be logged in to reply to this topic.