# python – JSONDecodeError: Expecting value: line 1 column 1 (char 0)

## The Question :

321 people think this question is useful

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

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


• Last but not least, what does print repr(response_json) tell you is being passed to .loads()?
• One more: why use simplejson when you can just use the stdlib json (which is the same library as simplejson)?
• That is an empty string. Your web_fetch() call failed.
• Yes, I do recommend you use something easier to use than pycurl. requests offers a far easier API, especially when it comes to debugging what is going on. Unless you specifically have to have a newer version of the simplejson library, just stick with json, saves you a dependency to manage.
• is response_json the return value of .json()? Then you already have decoded data and don’t need to use json.loads() anymore. response decoded it for you.

149 people think this answer is useful

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



77 people think this answer is useful

Check the response data-body, whether actual data is present and a data-dump appears to be well-formatted.

In most cases your json.loadsJSONDecodeError: 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('\'', '\"')
except:
print repr(resonse_json)
print sys.exc_info()



Note: Quotes within the data must be properly escaped

44 people think this answer is useful

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:



I’m a little embarrassed to admit that this can happen sometimes:

contents = json.loads(json_file_path)



43 people think this answer is useful

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.

25 people think this answer is useful

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:



15 people think this answer is useful

json.loads("file.json")



I solved the problem with

with open("file.json", "r") as read_file:



maybe this can help in your case

14 people think this answer is useful

A lot of times, this will be because the string you’re trying to parse is blank:

>>> import json
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:
else:
x = {}



6 people think this answer is useful

I encounterred the same problem, while print out the json string opened from a json file, found the json string starts with ‘ï»¿’, which by doing some reserach is due to the file is by default decoded with UTF-8, and by changing encoding to utf-8-sig, the mark out is stripped out and loads json no problem:

open('test.json', encoding='utf-8-sig')



4 people think this answer is useful

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', '')
except:
return struct



2 people think this answer is useful

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.

2 people think this answer is useful

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.

2 people think this answer is useful

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"



1 people think this answer is useful

For me, it was not using authentication in the request.

1 people think this answer is useful

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



1 people think this answer is useful

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



0 people think this answer is useful

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



0 people think this answer is useful

In my case it is because the server is giving http error occasionally. So basically once in a while my script gets the response like this rahter than the expected response:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>

Clearly this is not in json format and trying to call .json() will yield JSONDecodeError: Expecting value: line 1 column 1 (char 0)
You can print the exact response that causes this error to better debug. For example if you are using requests and then simply print the .text field (before you call .json()) would do.