JSON Parsing stops working when used with Flask on PythonAnywhere

I'm using Flask and PythonAnywhere to deploy a small new server which takes latitude/longitude arrays, sends this to the Bing Maps Snap-to-Road API which says that it would return a JSON file. However, the json.loads() always stops working ONLY when used on PythonAnywhere.

As of right now, I've tried the code on two local machines and it works fine. The PythonAnywhere instance is using Python 3.7, and I've tried the Bing Maps JSON code on both a Python 3.6 local machine and a Python 3.7 machine. I've even uploaded the file that I used for testing (not the entire server code, as that is fairly long), but the Bing Maps API code, onto PythonAnywhere and tested on their Python interpreter(both 3.7 and 3.6) and it seems to have worked fine, returning the two expected arrays.

import requests
lngArray = [array of longitudes stored as floats]
latArray = [array of latitudes stored as floats]
snappedLatArray = []
snappedLngArray = []

arrayLength = len(lngArray)
apiKey =“my-API-Key”
baseURL = “http://dev.virtualearth.net/REST/v1/Routes/SnapToRoad?points=
for i in range(0,arrayLength):
baseURL = baseURL+(str(latArray[i])[:9])+“,”+(str(lngArray[i])[:9])
if (i != (arrayLength - 1)):
baseURL = baseURL + “;”
elif(i == (arrayLength - 1)):
pass

finalURL = baseURL+“&interpolate=true&key=”+apiKey
print(finalURL)

c = requests.get(finalURL)
snappedPointsDict = c.json()
snappedPoints = snappedPointsDict[‘resourceSets’][0][‘resources’][0]
[‘snappedPoints’]
for x in snappedPoints:
snappedLat = x[‘coordinate’][‘latitude’]
snappedLng = x[‘coordinate’][‘longitude’]
snappedLatArray.append(snappedLat)
snappedLngArray.append(snappedLng)

On my machine and when run as a standalone program (such as the one above), it works flawlessly, and the snappedLatArray and snappedLngArray output arrays of latitudes and longitudes respectively. However, when run in a Flask environment, it fails, and in the error log it shows simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0) and the app stops working.

#python #json #flask

1 Likes3.65 GEEK