JSONDecodeError: Expecting value: line 1 column 1 (char 0)

匿名 (未验证) 提交于 2019-12-03 01:08:02

问题:

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)

回答1:

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()


回答2:

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



回答3:

With requests this 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.



回答4:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!