JSONDecodeError: Expected value: row 1 column 1 (char 0)

When trying to decode JSON, the error Expecting value: line 1 column 1 (char 0) appears.

The URL that I use to call the API works fine in the browser, but gives this error when the curl request is executed. Below is the code that I use for curl request.

The error occurs when 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 trace:

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) 
+147
json python api curl
May 15 '13 at 19:22
source share
8 answers

To summarize the conversation in the comments:

  • There is no need to use the simplejson library, the same library is included with Python as a json module.

  • There is no need to decode the response from UTF8 to unicode, the simplejson / json .loads() method can process UTF8 encoded data initially.

  • pycurl has a very archaic API. If you do not have special requirements for its use, there are more effective options.

requests offers the most user-friendly APIs, including JSON support. If you can, replace your call with:

 import requests return requests.get(url).json() 
+87
May 15 '13 at 21:13
source share
— -

Check the body of the response data for actual data, and the data dump looks well formatted.

In most cases, your json.loads error - JSONDecodeError: Expecting value: line 1 column 1 (char 0) is due to:

  • non-JSON matching quoting
  • XML / HTML output (i.e., a line starting with <), or
  • incompatible character encoding

Ultimately, the error tells you that in the very first position, the string no longer matches JSON.

Thus, if the parsing is not performed, despite the fact that the data body looks like JSON, as 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 inside the data must be properly shielded.

+47
Aug 27 '13 at 8:48 on
source share

With requests lib, a JSONDecodeError can occur if you have an http error code, such as 404, and you are trying to parse the response as JSON!

You must first check 200 (OK) or allow it to rise on error to avoid this case. Too bad this failed with a less cryptic error message.

NOTE : as Martijn Pieters noted in the comments, servers can respond with JSON in case of errors (this depends on the implementation), therefore, checking the Content-Type header is more reliable.

+23
May 29 '17 at 12:14
source share

0 can be embedded there, 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 
+3
Sep 29 '17 at 19:13
source share

I had exactly this problem using queries. Thanks to Christoph Russi for his explanation.

For debugging, I used:

 response = requests.get(url) logger.info(type(response)) 

I got a 404 response from the API.

+1
Sep 20 '18 at 15:12
source share

I had the same problem with queries (python library). This was the accept-encoding header.

It was set like this: 'accept-encoding': 'gzip, deflate, br'

I just deleted it from the request and stopped receiving the error.

0
Nov 19 '18 at 10:08
source share

check the encoding format of your file and use the appropriate encoding format when reading the file. This will solve your problem.

 with open("AB.json",encoding='utf-16', errors='ignore') as json_data: data = json.load(json_data, strict=False) 
0
Mar 06 '19 at 6:21
source share

This often happens because the line you are trying to parse is empty:

 >>> 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 fix len(json_string) pre-checking len(json_string) :

 import json if len(json_string) > 0: x = json.loads(json_string) else: // Your logic here x = {} 
0
May 6 '19 at 20:58
source share



All Articles