Invalid Control Character in JSON decode (using Python) - 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

Related

Python Json.decoder.JSONDecodeError: Expecting ',' delimiter:

I'm trying to load a dictionary into json but I'm getting error.
strsdc = '''
{"ext":{"amznregion":["useast"],"someURL":"https://som_url.com/x/px/Iyntynt/{"c":"client","als2s":TIME}","id":"7788y"}}
'''
json.loads(strsdc)
gives me the following error:
Traceback (most recent call last):
File "main.py", line 187, in
lol = json.loads(str(strsdc))
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/init.py", line 357, in loads
return _default_decoder.decode(s)
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 2 column 79 (char 79)
Your json string is invalid.
I don't know if this is the way you want it but you can try using this string
{"ext":{"amznregion":["useast"],"someURL":"https://som_url.com/x/px/Iyntynt/{\"c\":\"client\",\"als2s\":TIME}","id":"7788y"}}

Unable to read API responce data

I am trying to get data from the below seen URLs
import requests
import json
service_a = requests.get('https:xxxxx')
service_b = requests.get('https:xxxxx')
service_a.json()
service_b.json()
While for the case of service_a, service_a.json() gives me the expected data, for service_b, the call service_b.json() gives me the following error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3/dist-packages/requests/models.py", line 892, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 518, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
File "/usr/lib/python3/dist-packages/simplejson/scanner.py", line 79, in scan_once
return _scan_once(string, idx)
File "/usr/lib/python3/dist-packages/simplejson/scanner.py", line 70, in _scan_once
raise JSONDecodeError(errmsg, string, idx)
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Why is this happening?
ps. I notced that when I try to visit the https link of service_a, the data it contains are shown in the browser, while for sevice_b, no data are shown in the browser, but when I click it a file with its data is downloaded in my computer.
Try to check the Content-Type of service_b response.
import csv
response = requests.get(API_URL)
decoded_data = response.content.decode("utf-8")
csv_data = csv.reader(decoded_data.splitlines(), delimiters=",")
csv_list = list(csv_data)
in most cases your
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
error is due to:
non-JSON conforming quoting
XML/HTML output (that is, a string starting with <), or
incompatible character encoding

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"

json module bug in Python 3.4.1?

I'm trying to use SharePoint 2010's REST API, which was going swimmingly until i ran into this:
Traceback (most recent call last):
File "TestJSON.py", line 21, in <module>
json.loads(s)
File "c:\Python33\lib\json\__init__.py", line 316, in loads
return _default_decoder.decode(s)
File "c:\Python33\lib\json\decoder.py", line 351, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "c:\Python33\lib\json\decoder.py", line 367, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting ',' delimiter: line 1 column 14 (char 13)
Test case:
import json
s='''{"etag": "W/\"1\""}'''
json.loads(s)
Python 3.3.5 gives the same error. Did i find a bug in the JSON library?
Update:
The actual error i'm getting (preceded by affected part) is this:
>>>>>>Err:tration?$filter=Modified%20gt%20datetime\'2014-04-30T00:00:00.000Z\'&$orderby=Mo<<<<<<<
Traceback (most recent call last):
File "TestURL.py", line 41, in <module>
j = json.loads(body)
File "c:\Python33\lib\json\__init__.py", line 316, in loads
return _default_decoder.decode(s)
File "c:\Python33\lib\json\decoder.py", line 351, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "c:\Python33\lib\json\decoder.py", line 367, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Invalid \escape: line 4005 column 114 (char 314020)
from
body = response.read().decode("utf-8")
print(">>>>>>Err:{}<<<<<<<".format(body[314020-40:314020+40]))
The string literal is not escaped correctly. Make sure the string really represent JSON.
>>> s = r'''{"etag": "W/\"1\""}''' # NOTE: raw string literal
>>> json.loads(s)
{'etag': 'W/"1"'}
The \' sequence is invalid JSON. Single quotes do not need to be escaped, making this an invalid string escape.
You could try to repair it after the fact:
import re
data = re.sub(r"(?<!\\)\\'", "'", data)
before loading it with JSON. This replaces \' with plain ', provided the backslash wasn't already escaped by a preceding \.
Since single quotes can only appear in string values, this should be safe.

Google Maps Geocoding Error with Django

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...

Categories

Resources