-1

I am having issues pulling a JSON array from http://api.divesites.com. When I manually set the data as a string in Python 3.7, I can see the array.

    import json

def main():
    hardcode = """{"request":{"str":null,"timestamp":1572865590,"loc":{"lat":"50.442","lng":"-4.08279999999999"},"mode":"sites","dist":"9","api":1},"sites":[{"currents":null,"distance":"7.47","hazards":null,"lat":"50.3378","name":"Fort Bovisand","water":null,"marinelife":null,"description":null,"maxdepth":null,"mindepth":null,"predive":null,"id":"24387","equipment":null,"lng":"-4.1285"},{"currents":null,"distance":"7.93","hazards":null,"lat":"50.3352","name":"Plymouth Breakwater","water":null,"marinelife":null,"description":null,"maxdepth":null,"mindepth":null,"predive":null,"id":"24388","equipment":null,"lng":"-4.1485"}],"version":1,"loc":{"lat":"50.442","lng":"-4.08279999999999"},"result":true}
        """
    print("Original Data: ---", get_data(hardcode))
    print("Site Data:----", get_data(hardcode)['sites'])

def get_data(data):
    return json.loads(data)

if __name__=='__main__':
    main()

Output

Original Data: --- {'request': {'str': None, 'timestamp': 1572865590, 'loc': {'lat': '50.442', 'lng': '-4.08279999999999'}, 'mode': 'sites', 'dist': '9', 'api': 1}, 'sites': [{'currents': None, 'distance': '7.47', 'hazards': None, 'lat': '50.3378', 'name': 'Fort Bovisand', 'water': None, 'marinelife': None, 'description': None, 'maxdepth': None, 'mindepth': None, 'predive': None, 'id': '24387', 'equipment': None, 'lng': '-4.1285'}, {'currents': None, 'distance': '7.93', 'hazards': None, 'lat': '50.3352', 'name': 'Plymouth Breakwater', 'water': None, 'marinelife': None, 'description': None, 'maxdepth': None, 'mindepth': None, 'predive': None, 'id': '24388', 'equipment': None, 'lng': '-4.1485'}], 'version': 1, 'loc': {'lat': '50.442', 'lng': '-4.08279999999999'}, 'result': True}
Site Data:---- [{'currents': None, 'distance': '7.47', 'hazards': None, 'lat': '50.3378', 'name': 'Fort Bovisand', 'water': None, 'marinelife': None, 'description': None, 'maxdepth': None, 'mindepth': None, 'predive': None, 'id': '24387', 'equipment': None, 'lng': '-4.1285'}, {'currents': None, 'distance': '7.93', 'hazards': None, 'lat': '50.3352', 'name': 'Plymouth Breakwater', 'water': None, 'marinelife': None, 'description': None, 'maxdepth': None, 'mindepth': None, 'predive': None, 'id': '24388', 'equipment': None, 'lng': '-4.1485'}]

However when I try and perform a GET request (using urllib.request.urlopen()), I only receive the objects.

import urllib.request
import json

def get_jsonparsed_data(url):
    response = urllib.request.urlopen(url)
    data = response.read().decode("utf-8")
    return json.loads(data)

def main():
    url = 'http://api.divesites.com/?mode=sites&lat=-50.350874&lng=175.849890&dist=12'
    print("Original Data: ---", get_jsonparsed_data(url))
    print("Sites: ----", get_jsonparsed_data(url)['sites'])



if __name__=='__main__':
    main()

Output

Original Data: --- {'request': {'str': None, 'timestamp': 1572866211, 'loc': {'lat': '-50.350874', 'lng': '175.849890'}, 'mode': 'sites', 'dist': '12', 'api': 1}, 'sites': [], 'version': 1, 'loc': {'lat': '-50.350874', 'lng': '175.849890'}, 'result': True}
Site Data:---- []

Am I doing something wrong, or do I need an extra step?

3
  • 1
    stackoverflow.com/questions/12965203/… Commented Nov 4, 2019 at 11:46
  • see your Original Data - there is 'sites': [], - so you get empty list from server and Site Data shows you this empty list. All work correctly. Commented Nov 4, 2019 at 11:51
  • If I use lat=39.8333&lng=-98.5833 then code gives me many results in sites. The same for lat=50.442&lng=-4.08279999999999. Simply you have to use different values lat, lng to get some sites Commented Nov 4, 2019 at 12:00

2 Answers 2

1

Solved it. I spent so long focusing on debugging the code I forgot to check the final API call. Turns out I either didn't have enough distance set, or the wrong starting longitude and latitude. This was different to my hard coded JSON.

The URL should have been: http://api.divesites.com/?mode=sites&lat=50.442&lng=-4.08279999999999&dist=9

Stupid mistake. Thanks for helping though. It got me to look.

Sign up to request clarification or add additional context in comments.

Comments

0

First, I'd use the requests library instead and then you can find the solution here

1 Comment

problem has nothing to do with requests or JSON - code works correctly with different values lat, lng

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.