问题:JSONDecodeError:预期值:第1行第1列(字符0)
Expecting value: line 1 column 1 (char 0)
尝试解码JSON 时出现错误。
我用于API调用的URL在浏览器中可以正常工作,但是通过curl请求完成时会出现此错误。以下是我用于curl请求的代码。
错误发生在 return simplejson.loads(response_json)
response_json = self.web_fetch(url)
response_json = response_json.decode('utf-8')
return json.loads(response_json)
def web_fetch(self, url):
buffer = StringIO()
curl = pycurl.Curl()
curl.setopt(curl.URL, url)
curl.setopt(curl.TIMEOUT, self.timeout)
curl.setopt(curl.WRITEFUNCTION, buffer.write)
curl.perform()
curl.close()
response = buffer.getvalue().strip()
return response
完整回溯:
追溯:
File "/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/nab/Desktop/pricestore/pricemodels/views.py" in view_category
620. apicall=api.API().search_parts(category_id= str(categoryofpart.api_id), manufacturer = manufacturer, filter = filters, start=(catpage-1)*20, limit=20, sort_by='[["mpn","asc"]]')
File "/Users/nab/Desktop/pricestore/pricemodels/api.py" in search_parts
176. return simplejson.loads(response_json)
File "/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/simplejson/__init__.py" in loads
455. return _default_decoder.decode(s)
File "/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/simplejson/decoder.py" in decode
374. obj, end = self.raw_decode(s)
File "/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/simplejson/decoder.py" in raw_decode
393. return self.scan_once(s, idx=_w(s, idx).end())
Exception Type: JSONDecodeError at /pricemodels/2/dir/
Exception Value: Expecting value: line 1 column 1 (char 0)
I am getting error Expecting value: line 1 column 1 (char 0)
when trying to decode JSON.
The URL I use for the API call works fine in the browser, but gives this error when done through a curl request. The following is the code I use for the curl request.
The error happens at return simplejson.loads(response_json)
response_json = self.web_fetch(url)
response_json = response_json.decode('utf-8')
return json.loads(response_json)
def web_fetch(self, url):
buffer = StringIO()
curl = pycurl.Curl()
curl.setopt(curl.URL, url)
curl.setopt(curl.TIMEOUT, self.timeout)
curl.setopt(curl.WRITEFUNCTION, buffer.write)
curl.perform()
curl.close()
response = buffer.getvalue().strip()
return response
Full Traceback:
Traceback:
File "/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/nab/Desktop/pricestore/pricemodels/views.py" in view_category
620. apicall=api.API().search_parts(category_id= str(categoryofpart.api_id), manufacturer = manufacturer, filter = filters, start=(catpage-1)*20, limit=20, sort_by='[["mpn","asc"]]')
File "/Users/nab/Desktop/pricestore/pricemodels/api.py" in search_parts
176. return simplejson.loads(response_json)
File "/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/simplejson/__init__.py" in loads
455. return _default_decoder.decode(s)
File "/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/simplejson/decoder.py" in decode
374. obj, end = self.raw_decode(s)
File "/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/simplejson/decoder.py" in raw_decode
393. return self.scan_once(s, idx=_w(s, idx).end())
Exception Type: JSONDecodeError at /pricemodels/2/dir/
Exception Value: Expecting value: line 1 column 1 (char 0)
回答 0
总结评论中的对话:
无需使用simplejson
库,Python作为json
模块包含了相同的库。
无需解码UTF8对unicode的响应,simplejson
/ json
.loads()
方法可以本地处理UTF8编码的数据。
pycurl
有一个非常古老的API。除非您有特定的使用要求,否则会有更好的选择。
requests
提供最友好的API,包括JSON支持。如果可以,将您的通话替换为:
import requests
return requests.get(url).json()
To summarize the conversation in the comments:
There is no need to use simplejson
library, the same library is included with Python as the json
module.
There is no need to decode a response from UTF8 to unicode, the simplejson
/ json
.loads()
method can handle UTF8 encoded data natively.
pycurl
has a very archaic API. Unless you have a specific requirement for using it, there are better choices.
requests
offers the most friendly API, including JSON support. If you can, replace your call with:
import requests
return requests.get(url).json()
回答 1
检查响应数据主体,是否存在实际数据并且数据转储的格式是否正确。
在大多数情况下,您的json.loads
– JSONDecodeError: Expecting value: line 1 column 1 (char 0)
错误是由于:
- 非JSON引用
- XML / HTML输出(即以<开头的字符串),或
- 不兼容的字符编码
最终,错误告诉您字符串在第一位置已经不符合JSON。
这样,如果尽管乍一看具有看起来像JSON的数据主体,但解析仍然失败,请尝试替换数据主体的引号:
import sys, json
struct = {}
try:
try: #try parsing to dict
dataform = str(response_json).strip("'<>() ").replace('\'', '\"')
struct = json.loads(dataform)
except:
print repr(resonse_json)
print sys.exc_info()
注意:数据中的引号必须正确转义
Check the response data-body, whether actual data is present and a data-dump appears to be well-formatted.
In most cases your json.loads
– 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
Ultimately the error tells you that at the very first position the string already doesn’t conform to JSON.
As such, if parsing fails despite having a data-body that looks JSON like at first glance, try replacing the quotes of the data-body:
import sys, json
struct = {}
try:
try: #try parsing to dict
dataform = str(response_json).strip("'<>() ").replace('\'', '\"')
struct = json.loads(dataform)
except:
print repr(resonse_json)
print sys.exc_info()
Note: Quotes within the data must be properly escaped
回答 2
当您有一个HTTP错误代码(例如404)并尝试将响应解析为JSON时,使用requests
lib JSONDecodeError
可能会发生!
您必须首先检查200(OK)或让它出现错误以避免这种情况。我希望它以较少的错误消息失败。
注意:就像注释中提到的Martijn Pieters一样,如果发生错误(取决于实现),服务器可以使用JSON进行响应,因此检查Content-Type
标头更加可靠。
With the requests
lib JSONDecodeError
can happen when you have an http error code like 404 and try to parse the response as JSON !
You must first check for 200 (OK) or let it raise on error to avoid this case.
I wish it failed with a less cryptic error message.
NOTE: as Martijn Pieters stated in the comments servers can respond with JSON in case of errors (it depends on the implementation), so checking the Content-Type
header is more reliable.
回答 3
我认为值得一提的是,在您解析JSON文件本身的内容时-健全性检查对于确保您实际上在调用文件json.loads()
的内容(而不是该JSON 的文件路径)非常有用。:
json_file_path = "/path/to/example.json"
with open(json_file_path, 'r') as j:
contents = json.loads(j.read())
我有些尴尬地承认有时会发生这种情况:
contents = json.loads(json_file_path)
I think it’s worth mentioning that in cases where you’re parsing the contents of a JSON file itself – sanity checks can be useful to ensure that you’re actually invoking json.loads()
on the contents of the file, as opposed to the file path of that JSON:
json_file_path = "/path/to/example.json"
with open(json_file_path, 'r') as j:
contents = json.loads(j.read())
I’m a little embarrassed to admit that this can happen sometimes:
contents = json.loads(json_file_path)
回答 4
检查文件的编码格式,并在读取文件时使用相应的编码格式。它将解决您的问题。
with open("AB.json", encoding='utf-8', errors='ignore') as json_data:
data = json.load(json_data, strict=False)
Check encoding format of your file and use corresponding encoding format while reading file. It will solve your problem.
with open("AB.json", encoding='utf-8', errors='ignore') as json_data:
data = json.load(json_data, strict=False)
回答 5
很多时候,这是因为您要解析的字符串为空:
>>> import json
>>> x = json.loads("")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/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)
您可以通过json_string
事先检查是否为空来进行补救:
import json
if json_string:
x = json.loads(json_string)
else:
// Your logic here
x = {}
A lot of times, this will be because the string you’re trying to parse is blank:
>>> import json
>>> x = json.loads("")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/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)
You can remedy by checking whether json_string
is empty beforehand:
import json
if json_string:
x = json.loads(json_string)
else:
// Your logic here
x = {}
回答 6
即使调用了decode(),也可能嵌入了0。使用replace():
import json
struct = {}
try:
response_json = response_json.decode('utf-8').replace('\0', '')
struct = json.loads(response_json)
except:
print('bad json: ', response_json)
return struct
There may be embedded 0’s, even after calling decode(). Use replace():
import json
struct = {}
try:
response_json = response_json.decode('utf-8').replace('\0', '')
struct = json.loads(response_json)
except:
print('bad json: ', response_json)
return struct
回答 7
使用请求,我遇到了这个问题。感谢Christophe Roussy的解释。
为了调试,我使用了:
response = requests.get(url)
logger.info(type(response))
我从API返回了404响应。
I had exactly this issue using requests.
Thanks to Christophe Roussy for his explanation.
To debug, I used:
response = requests.get(url)
logger.info(type(response))
I was getting a 404 response back from the API.
回答 8
我在请求(python库)时遇到了同样的问题。它恰好是accept-encoding
标题。
它是这样设置的: 'accept-encoding': 'gzip, deflate, br'
我只是将其从请求中删除,并停止获取错误。
I was having the same problem with requests (the python library). It happened to be the accept-encoding
header.
It was set this way: 'accept-encoding': 'gzip, deflate, br'
I simply removed it from the request and stopped getting the error.
回答 9
For me, it was not using authentication in the request.
回答 10
对我来说,这是服务器响应而不是200响应,并且响应不是json格式的。我最终在json解析之前执行了此操作:
# this is the https request for data in json format
response_json = requests.get()
# only proceed if I have a 200 response which is saved in status_code
if (response_json.status_code == 200):
response = response_json.json() #converting from json to dictionary using json library
For me it was server responding with something other than 200 and the response was not json formatted. I ended up doing this before the json parse:
# this is the https request for data in json format
response_json = requests.get()
# only proceed if I have a 200 response which is saved in status_code
if (response_json.status_code == 200):
response = response_json.json() #converting from json to dictionary using json library
回答 11
如果您是Windows用户,则Tweepy API可以在数据对象之间生成一个空行。由于这种情况,您会收到“ JSONDecodeError:预期值:第1行第1列(字符0)”错误。为避免此错误,您可以删除空行。
例如:
def on_data(self, data):
try:
with open('sentiment.json', 'a', newline='\n') as f:
f.write(data)
return True
except BaseException as e:
print("Error on_data: %s" % str(e))
return True
参考:
Twitter流API从None给出JSONDecodeError(“ Expecting value”,s,err.value)
If you are a Windows user, Tweepy API can generate an empty line between data objects. Because of this situation, you can get “JSONDecodeError: Expecting value: line 1 column 1 (char 0)” error. To avoid this error, you can delete empty lines.
For example:
def on_data(self, data):
try:
with open('sentiment.json', 'a', newline='\n') as f:
f.write(data)
return True
except BaseException as e:
print("Error on_data: %s" % str(e))
return True
Reference:
Twitter stream API gives JSONDecodeError(“Expecting value”, s, err.value) from None
回答 12
只需检查请求的状态码是否为200。例如:
if status != 200:
print("An error has occured. [Status code", status, "]")
else:
data = response.json() #Only convert to Json when status is OK.
if not data["elements"]:
print("Empty JSON")
else:
"You can extract data here"
Just check if the request has a status code 200. So for example:
if status != 200:
print("An error has occured. [Status code", status, "]")
else:
data = response.json() #Only convert to Json when status is OK.
if not data["elements"]:
print("Empty JSON")
else:
"You can extract data here"
回答 13
我在基于Python的Web API的响应中收到了这样的错误.text
,但是它导致了我的到来,因此这可能会帮助遇到类似问题的其他人(使用requests
.. 过滤响应并在搜索中请求问题非常困难)
使用json.dumps()
在要求 data
ARG创建JSON的正确转义的字符串前发布解决了该问题对我来说
requests.post(url, data=json.dumps(data))
I received such an error in a Python-based web API’s response .text
, but it led me here, so this may help others with a similar issue (it’s very difficult to filter response and request issues in a search when using requests
..)
Using json.dumps()
on the request data
arg to create a correctly-escaped string of JSON before POSTing fixed the issue for me
requests.post(url, data=json.dumps(data))