Google Maps Geocoding Error with Django - python

After hours of trying to figure out the problem here, I've given up. I am using http://djangosnippets.org/snippets/2241/ (saved in myproject/custom.py) to query Google and return the co-ordinates of the location. The beginning of my function is:
def locQuery(location **kwargs):
"""
This function returns a queryset of locations based on a Google Geocode
lookup and other parameters
"""
q = GoogleLatLng()
query = MyModel.objects.none()
try:
q.requestLatLngJSON(location)
except:
return query
# Do something based on kwargs #
However, I get the following error intermittently:
>>> locQuery('York')
Traceback (most recent call last):
File "/home/../myproject/custom.py", line 85, in parseResults
self.results = json.loads(buff)
File "/usr/local/lib/python2.7/json/__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python2.7/json/decoder.py", line 360, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/lib/python2.7/json/decoder.py", line 376, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting object: line 37 column 8 (char 1133)
[]
The following is an example of the code working as it should:
>>> locQuery('York')
[]
Any help at all would be wonderful.
EDIT:
So I've finally decided that the snippet itself is faulty. I am now using the python code available on http://code.google.com/apis/maps/documentation/webservices/index.html#ParsingJSON to perform my Geocoding queries...

Related

JSON decode error since I switched from macOS to Windows

I have a Python file that worked perfectly on my macbook.
Once I moved it to window, for some reason it gave me new error.
this is the errors im getting:
Traceback (most recent call last):
File "C:/Users/chadi/PycharmProjects/untitled4/main.py", line 3, in <module>
from main_controller import Ui_MainController
File "C:\Users\chadi\PycharmProjects\untitled4\main_controller.py", line 6, in <module>
from get_companies import load_companies
File "C:\Users\chadi\PycharmProjects\untitled4\get_companies.py", line 10, in <module>
json_read('convertcsv.json')
File "C:\Users\chadi\PycharmProjects\untitled4\get_companies.py", line 8, in json_read
data = (json.load(f_in))
File "C:\Users\chadi\anaconda3\envs\untitled4\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Users\chadi\anaconda3\envs\untitled4\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\chadi\anaconda3\envs\untitled4\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\chadi\anaconda3\envs\untitled4\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Process finished with exit code 1
this is all the parts where i load a JSON file:
def json_read(filename):
with open(filename) as f_in:
global data
data = (json.load(f_in))
json_read('convertcsv.json')
def mark_employee_unavailability(service,employee,reason,start_date,end_date):
with open('info_inspecteurs.json') as json_file:
data = json.load(json_file)
for i in range(len(data)):
if data[i]['n_inspecteur'] == employee:
event_email = data[i]['email_inspecteur']
break
def json_read(filename):
with open(filename) as f_in:
global data
data = (json.load(f_in))
def get_employee_info(n_inspecteur):
json_read('info_inspecteurs.json')
value = list(filter(lambda x: x["n_inspecteur"] == n_inspecteur, data))[0]
if len(value) > 0:
print(value['ad_inspecteur'], "\n", value['email_inspecteur'])
return (value['ad_inspecteur'],value['email_inspecteur'])
print("no record found")
return None
def load_employees_from_info_inspecteurs():
json_read('info_inspecteurs.json')
employees=[]
for company in data:
employees.append(company['n_inspecteur'])
return employees
I don't know where it came from, or maybe is the new environement I used on Windows? I used anaconda. Does it change anything? I was on VENV on my macOS.
Thank you for your help
The error
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
may just mean that there's no data being read from the target file at all, so json isn't finding any data to parse.
You might troubleshoot by trying to just read the file from that point in your script into a string - if it fails, the problem may be in python execution current directory / relative file path, rather than in the json parsing.
with open ("convertcsv.json", "r") as checkFile:
checkData = checkFile.read()
# print out length or contents of checkData or sim.

Reading JSON file using Python

I have a JSON file called 'elements.json':
[
{ldraw="003238a",lgeo="003238a",slope=0,anton=0,lutz=0,owen=0,damien=0},
{ldraw="003238b",lgeo="003238b",slope=0,anton=0,lutz=0,owen=0,damien=0},
{ldraw="003238c",lgeo="003238c",slope=0,anton=0,lutz=0,owen=0,damien=0},
{ldraw="003238d",lgeo="003238d",slope=0,anton=0,lutz=0,owen=0,damien=0}
]
I have a Python file called 'test.py':
import json
with open('elements.json') as json_file:
data = json.load(json_file)
for p in data:
print('ldraw: ' + p['ldraw'])
print('lgeo: ' + p['lgeo'])
Running from the Windows command line I get this error:
Traceback (most recent call last):
File "test.py", line 4, in <module>
data = json.load(json_file)
File "C:\Python27\lib\json\__init__.py", line 278, in load
**kw)
File "C:\Python27\lib\json\__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 2 column 2 (char 3)
What property name is expected? Why am I getting the error?
You aren't following the JSON specification. See json.org for details.
[
{"ldraw":"003238a","lgeo":"003238a","slope":0,"anton":0,"lutz":0,"owen":0,"damien":0},
{"ldraw":"003238b","lgeo":"003238b","slope":0,"anton":0,"lutz":0,"owen":0,"damien":0},
{"ldraw":"003238c","lgeo":"003238c","slope":0,"anton":0,"lutz":0,"owen":0,"damien":0},
{"ldraw":"003238d","lgeo":"003238d","slope":0,"anton":0,"lutz":0,"owen":0,"damien":0}
]
Your Python code is correct.
Your ldraw and lgeo values look like hexadecimal; JSON does not support hex, and you will have to do the extra work yourself.
[Edit: They're not]
Your file elements.json is not a valid json file.
It should have looked like this -
[{"ldraw":"003238a","lgeo":"003238a"}]
Your JSON format is invalid, JSON stands for JavaScript Object Notation, like the Javascript Object. So, you should replace "=" to ":". It means key-value pairs.
Wrong:
ldraw="003238a"
ldraw: 003238a // if no quote, the value should be the digit only.
Right:
ldraw: "003238a"
ldraw: { "example-key": "value" }
ldraw: "True"

Error in reading JSON: No JSON object could be decoded

I am reading a set of JSON files using glob and storing them in a list. The length of the list is 1046. When I am reading the JSON file one by one and loading it to run further code, it just runs on 595 files and gives the following error:
Traceback (most recent call last):
File "removeDeleted.py", line 38, in <module>
d = json.load(open(fn))
File "/usr/lib/python2.7/json/__init__.py", line 291, in load
**kw)
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
I am loading the json files like this:
json_file_names = sorted(glob.glob("./Intel_Shared_Data/gtFine/train/*/*.json"))
for fn in json_file_names:
#print fn
#temp = temp + 1
#count = 0
d = json.load(open(fn))
objects = d["objects"]
for j in range(len(objects)):
Can anybody suggest me way out of this error?
As Blender said, you need to find out which of your files contains invalid JSON. To this end, you need to add some debugging statements to your code:
json_file_names = sorted(glob.glob("./Intel_Shared_Data/gtFine/train/*/*.json"))
for fn in json_file_names:
#print fn
#temp = temp + 1
#count = 0
try:
d = json.load(open(fn))
objects = d["objects"]
for j in range(len(objects)):
except ValueError as e:
print "Could not load {}, invalid JSON".format({})
One of your json text files is empty. Maybe start by seeing if you have any zero size files with
find . -size 0
run from your directory of json files in a terminal.

Creating dataframe from json not always working

I'm trying to run this code to create a data frame from a JSON link. Sometimes, the code will run. Other times, I will get an error message (below). I'm not sure why this occurs, even though the code is the same.
import requests
import json
url = "http://stats.nba.com/stats/leaguedashplayerstats?College=&Conference=&Country=&DateFrom=&DateTo=&Division=&DraftPick=&DraftYear=&GameScope=&GameSegment=&Height=&LastNGames=0&LeagueID=00&Location=&MeasureType=Advanced&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=Totals&Period=0&PlayerExperience=&PlayerPosition=&PlusMinus=N&Rank=N&Season=2016-17&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&StarterBench=&TeamID=0&VsConference=&VsDivision=&Weight="
jd = requests.get(url).json()
df = []
for item in requests.get(url).json()['resultSets']:
print("got here")
row_df = []
for row in item['rowSet']:
row_df.append(str(row).strip('[]'))
df.append("\n")
df.append(row_df)
print(df)
Error Message:
Traceback (most recent call last):
File "/Users/K/PycharmProjects/mousefun/fun", line 8, in <module>
jd = requests.get(url).json()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/models.py", line 812, in json return complexjson.loads(self.text, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/__init__.py", line 318, in loads return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/decoder.py", line 343, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/decoder.py", line 361, in raw_decode raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)
Change your request logic to this and try again:
r = requests.get(url)
r.raise_for_status()
df = []
for item in r.json()["resultSets"]:
# ...
r.raise_for_status() will raise if the status is not OK .
Also, this does not do the request two times like your code does.

Invalid Control Character in JSON decode (using Python)

def list(type, extra=""):
if extra != "":
entity = "http://api.crunchbase.com/v/1/" + type + "/" + extra + ".js?api_key=" + key
data = json.load(urllib2.urlopen(entity))
else:
entity = "http://api.crunchbase.com/v/1/" + type + ".js?api_key=" + key
data = json.load(urllib2.urlopen(entity))
return data
The function list is called specifically here:
x = colink
details = list(co, x)
specifically on the instance where x is "if_this_then_that" and co is "company"
The code breaks down on this line when I query on the second line (the entity link is properly formatted). The Error Message is below and the line in the JSON file where the error occurs follows. I am not sure how to handle the unicode error when getting data through a JSON API. Any suggestions on how to remedy this would be appreciated.
Traceback (most recent call last):
File "crunch_API.py", line 95, in <module>
details = list(co, x)
File "crunch_API.py", line 34, in list
data = json.load(urllib2.urlopen(entity))
File "C:\Python27\lib\json\__init__.py", line 278, in load
**kw)
File "C:\Python27\lib\json\__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Invalid control character at: line 24 column 89 (char 881)
"overview": "\u003Cp\u003EIFTTT is a service that lets you create powerful connections with one simple statement: if this then that.\u003C/p\u003E", #### Where the error occurs

Categories

Resources