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
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)
asked May 15, 2013 at 19:22
user1328021user1328021
8,88014 gold badges46 silver badges76 bronze badges
11
Your code produced an empty response body, you’d want to check for that or catch the exception raised. It is possible the server responded with a 204 No Content response, or a non-200-range status code was returned (404 Not Found, etc.). Check for this.
Note:
-
There is no need to use
simplejson
library, the same library is included with Python as thejson
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.
Either the requests
or httpx
offers much friendlier APIs, including JSON support. If you can, replace your call with:
import requests
response = requests.get(url)
response.raise_for_status() # raises exception when not a 2xx response
if response.status_code != 204:
return response.json()
Of course, this won’t protect you from a URL that doesn’t comply with HTTP standards; when using arbirary URLs where this is a possibility, check if the server intended to give you JSON by checking the Content-Type header, and for good measure catch the exception:
if (
response.status_code != 204 and
response.headers["content-type"].strip().startswith("application/json")
):
try:
return response.json()
except ValueError:
# decide how to handle a server that's misbehaving to this extent
answered May 15, 2013 at 21:13
Martijn Pieters♦Martijn Pieters
1.0m295 gold badges4025 silver badges3320 bronze badges
1
Be sure to remember to invoke 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 think a lot of people are guilty of doing this every once in a while (myself included):
contents = json.load(json_file_path)
Wyrmwood
3,23029 silver badges33 bronze badges
answered Oct 31, 2019 at 16:13
3
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
answered Aug 27, 2013 at 8:48
Lorenz Lo SauerLorenz Lo Sauer
23.5k16 gold badges84 silver badges87 bronze badges
4
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.
answered May 29, 2017 at 12:14
3
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)
answered Mar 6, 2019 at 6:21
2
I had the same issue trying to read json files with
json.loads("file.json")
I solved the problem with
with open("file.json", "r") as read_file:
data = json.load(read_file)
maybe this can help in your case
answered Jul 9, 2020 at 13:23
GaluoisesGaluoises
2,49222 silver badges28 bronze badges
1
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 code/logic here
x = {}
answered May 6, 2019 at 20:58
Alex WAlex W
37k13 gold badges106 silver badges109 bronze badges
4
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')
answered Jun 23, 2020 at 21:00
user9571515user9571515
2895 silver badges14 bronze badges
1
This is the minimalist solution I found when you want to load json file in python
import json
data = json.load(open('file_name.json'))
If this give error saying character doesn’t match on position X and Y, then just add encoding='utf-8'
inside the open
round bracket
data = json.load(open('file_name.json', encoding='utf-8'))
Explanation
open
opens the file and reads the containts which later parse inside json.load
.
Do note that using with open() as f
is more reliable than above syntax, since it make sure that file get closed after execution, the complete sytax would be
with open('file_name.json') as f:
data = json.load(f)
answered Oct 1, 2021 at 7:58
There may be embedded 0’s, even after calling decode(). Use replace():
import json
struct = {}
try:
response_json = response_json.decode('utf-8').replace('', '')
struct = json.loads(response_json)
except:
print('bad json: ', response_json)
return struct
answered Sep 29, 2017 at 19:13
bryanbryan
1301 silver badge6 bronze badges
2
I had the same issue, in my case I solved like this:
import json
with open("migrate.json", "rb") as read_file:
data = json.load(read_file)
answered May 2, 2021 at 9:50
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.
answered Nov 19, 2018 at 10:08
Seu MadrugaSeu Madruga
3257 silver badges13 bronze badges
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"
answered May 29, 2020 at 13:36
Wout VCWout VC
3271 gold badge4 silver badges8 bronze badges
In my case I was doing file.read() two times in if and else block which was causing this error. so make sure to not do this mistake and hold contain in variable and use variable multiple times.
answered Nov 23, 2021 at 8:01
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.
answered Sep 20, 2018 at 15:12
Kelsie CKelsie C
411 silver badge4 bronze badges
2
In my case it occured because i read the data of the file using file.read()
and then tried to parse it using json.load(file)
.I fixed the problem by replacing json.load(file)
with json.loads(data)
Not working code
with open("text.json") as file:
data=file.read()
json_dict=json.load(file)
working code
with open("text.json") as file:
data=file.read()
json_dict=json.loads(data)
answered Apr 10, 2022 at 6:01
4
For me, it was not using authentication in the request.
answered Dec 12, 2019 at 16:34
Neel0507Neel0507
1,0942 gold badges11 silver badges18 bronze badges
I did:
- Open
test.txt
file, write data - Open
test.txt
file, read data
So I didn’t close file after 1.
I added
outfile.close()
and now it works
answered Jul 19, 2021 at 23:09
parsecerparsecer
4,66812 gold badges67 silver badges137 bronze badges
1
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
answered Jan 5, 2020 at 19:43
FastGTRFastGTR
3434 silver badges4 bronze badges
1
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))
answered Jun 12, 2020 at 18:54
ti7ti7
15.7k6 gold badges39 silver badges67 bronze badges
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>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<h1>502 Bad Gateway</h1>
<p>The proxy server received an invalid response from an upstream server.<hr/>Powered by Tengine</body>
</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.
answered Dec 27, 2020 at 5:41
Qin HeyangQin Heyang
1,3861 gold badge16 silver badges17 bronze badges
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
answered Mar 13, 2020 at 20:08
drorhundrorhun
4657 silver badges22 bronze badges
1
if you use headers and have "Accept-Encoding": "gzip, deflate, br"
install brotli library with pip install. You don’t need to import brotli to your py file.
answered Dec 31, 2021 at 20:16
JSONDecodeError: Expecting value: line 1 column 1 (char 0) occurs while working with JSON (JavaScript Object Notation) format. You might be storing some data or trying to fetch JSON data from an API(Application Programming Interface). In this guide, we will discuss where it can creep in and how to resolve it.
JSONDecodeError means there is an incorrect JSON format being followed. For instance, the JSON data may be missing a curly bracket, have a key that does not have a value, and data not enclosed within double-quotes or some other syntactic error.
Generally, the error expecting value: line 1 column 1 (char 0) error can occur due to the following reasons.
- Non-JSON conforming quoting
- Empty JSON file
- XML output (that is, a string starting with <)
Let’s elaborate on the points stated above:
1. Non-JSON conforming quoting
JSON or JavaScript Object Notation has a similar format to that of the python dictionary datatype. A dictionary requires a key or value to be enclosed in quotes if it is a string. Similarly, the JSON standard defines that keys need not be a string. However, keys should always be enclosed within double quotes. Not following this standard can also raise an error.
2. Empty JSON file
For this example, we have taken an empty JSON file named test.py and another file named test.py, which contains the code given below. Using context manager, we open the JSON file in reading mode and load it using the load method. However, an error is thrown because the JSON file is empty.
import json with open("test.json", "r") as file: data = json.load(file) print("Data retrieved")
3. XML output (that is, a string starting with <)
The Extensible Markup Language, or simply XML, is a simple text-based format for representing structured information: documents, data, configuration, books, transactions, invoices, and much more. Similar to JSON, it is an older way of storing data. Earlier APIs used to return data in XML format; however, JSON is nowadays the preferred choice. Let’s see how we can face the expecting value: line 1 column 1 (char 0) type error in this case.
<part number="1976"> <name>Windscreen Wiper</name> <description>The Windscreen wiper automatically removes rain from your windscreen, if it should happen to splash there. It has a rubber <ref part="1977">blade</ref> which can be ordered separately if you need to replace it. </description> </part>
import json with open("test.xml", "r") as file: data = json.load(file) print("Data retrieved")
Let’s break down what is happening here.
- For ease of example, suppose API returns an XML format data, as shown above.
- Now, when we try to load that response from API, we will get a type error.
Resolving JSONDecodeError: Expecting value: line 1 column 1 (char 0)
1. Solution for Non-JSON conforming quoting
To resolve the type error in this case, you need to ensure that the keys and values are enclosed within the double quotes. This is necessary because it is a part of JSON syntax. It is important to realize that JSON only uses double quotes, not single quotes.
2. Solution for empty JSON file
The solution for this is self-evident in the name. To resolve the type error, in this case, make sure that the JSON file or the response from an API is not empty. If the file is empty, add content to it, and for a response from an API, use try-except to handle the error, which can be empty JSON or a 404 error, for instance.
import json import requests def main(): URL = "https://api.dictionaryapi.dev/api/v2/enties/en/" word = input("Enter a word:") data = requests.get(url + word) data = data.text try: data_json = json.loads(data) print(data_json) except json.JSONDecodeError: print("Empty response") if __name__ == "__main__": main()
The above code takes in a word and returns all the information related to it in a JSON format. Now in order to show how we can handle the Expecting value: line 1 column 1 (char 0) type error, we have altered the URL. entries have been changed to enties. Therefore we will get an invalid response which will not be of the JSON format. However, this is merely done to imitate the error when you might get an invalid response from an API.
- Now, if you try to run the code above, you will be prompted to enter a word. The response is saved into the data variable and later converted to a string.
- However, in the try block json.loads method, which parses JSON string to python dictionary raises an error.
- This is because the response sent by API is not of JSON format, hence can’t be parsed, resulting in JSONDecodeError.
- JSONDecodeError gets handled by the try-except block, and a “Response content is not valid JSON” gets printed as an outcome.
3. Solution for XML output(that is, a string starting with <)
To avoid type errors resulting from an XML format, we will convert it to a JSON format. However, firstly install this library.
pip install xmltodict
import json import xmltodict with open("test.xml", "r") as file: data = xmltodict.parse(file.read()) file.close() json_data = json.dumps(data) with open("t.json", "w") as json_file: json_file.write(json_data) json_file.close() print("Data retrieved") print(data)
Let’s elaborate on the code above:
- We have imported two libraries, namely JSON and xmltodict.
- Using the context manager with, XML file test.xml is opened in read mode. Thereafter using the xmltodict parse method, it is converted to a dictionary type, and the file is closed.
- json.dumps() takes in a JSON object and returns a string of that object.
- Again using context manager with, a JSON file is created, XML data that was converted to a JSON string is written on it, and the file is closed.
JSONDecodeError: Expecting value: line 1 column 1 (char 0) Django
This issue is caused by the failure of Pipenv 2018.10.9. To resolve this issue, use Pipenv 2018.5.18. For more, read here.
Are you facing this error in Flask?
Many times, when you receive the data from HTTP requests, they are fetched as bytes. So, if you face JSONDecodeError, you might be dealing with a JSON in bytes. First, you need to decode the JSON by using response.decode('utf-8')
. This will create a string, and then you can parse it.
FAQs
How to parse JSON in python?
json.loads() method can be used to parse JSON in python. For instance:import json
json_string = '{"a":"1", "b":"2", "c":"3"}'
json_to_dict = json.loads(json_string)
print(json_to_dict)
print(type(json_to_dict))
How to detect empty file/string before parsing JSON?
import os
)
file_path = "/home/nikhilomkar/Desktop/test.json"
print("File is empty!" if os.stat(file_path).st_size == 0 else "File isn't empty!"
The following code checks if the file is empty and prints the File is empty! If true, else, the File isn’t empty!.
Conclusion JSONDecodeError: Expecting value: line 1 column 1 (char 0)
The following article discussed the JSONDecodeError: Expecting value: line 1 column 1 (char 0). This error is due to various decoding and formatting errors. We looked at likely situations where it can occur and how to resolve it.
Trending Python Articles
-
[Fixed] SSL module in Python is Not Available
●May 30, 2023
-
Mastering Python Translate: A Beginner’s Guide
by Namrata Gulati●May 30, 2023
-
Efficiently Organize Your Data with Python Trie
by Namrata Gulati●May 2, 2023
-
[Fixed] modulenotfounderror: no module named ‘_bz2
by Namrata Gulati●May 2, 2023
If you try to parse invalid JSON or decode an empty string as JSON, you will encounter the JSONDecodeError: Expecting value: line 1 column 1 (char 0). This error can occur if you read an empty file using json.load, read an empty JSON or receive an empty response from an API call.
You can use a try-except code block to catch the error and then check the contents of the JSON string or file before retrying.
This tutorial will go through the error in detail and how to solve it with code examples.
Table of contents
- JSONDecodeError: Expecting value: line 1 column 1 (char 0)
- Example #1: Incorrect use of json.loads()
- Solution
- Example #2: Empty JSON file
- Solution
- Example #3: Response Request
- Summary
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
In Python, JSONDecodeError occurs when there is an issue with the formatting of the JSON data. This specific error tells us the JSON decoder has encountered an empty JSON.
Example #1: Incorrect use of json.loads()
Let’s look at an example where we have a JSON file with the following contents:
[ {"margherita":7.99}, {"pepperoni":9.99}, {"four cheeses":10.99} ]
We want to read the data into a program using the json
library. Let’s look at the code:
import json json_path = 'pizza.json' data = json.loads(json_path)
In the above code, we try to read the data in using json.loads()
. Let’s run the code to see the result:
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
The error occurs because json.loads()
expects a JSON encoded string, not a filename. The string pizza.json
is not a valid JSON encoded string.
Solution
We need to use json.load()
instead of json.loads()
to read a file. Let’s look at the revised code:
import json json_path = 'pizza.json' with open(json_path, 'r') as f: data = json.loads(f.read()) print(data)
In the above code, we use the open()
function to create a file object that json.load()
can read and return the decoded data object. The with
statement is a context manager that ensures that the file is closed once the code is complete. Let’s run the code to see the result:
[{'margherita': 7.99}, {'pepperoni': 9.99}, {'four cheeses': 10.99}]
Example #2: Empty JSON file
Let’s look at an example where we have an empty file, which we will try to read in using json.loads()
. The file is called particles.json
. Since the JSON file is empty, the JSON decoder will throw the JSONDecodeError when it tries to read the file’s contents. Let’s look at the code:
import json filename = 'particles.json' with open(filename, 'r') as f: contents = json.loads(f.read()) print(contents)
--------------------------------------------------------------------------- JSONDecodeError Traceback (most recent call last) Input In [1], in <cell line: 5>() 3 filename = 'particles.json' 5 with open(filename, 'r') as f: ----> 6 contents = json.loads(f.read()) 7 print(contents) File ~/opt/anaconda3/lib/python3.8/json/__init__.py:357, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw) 352 del kw['encoding'] 354 if (cls is None and object_hook is None and 355 parse_int is None and parse_float is None and 356 parse_constant is None and object_pairs_hook is None and not kw): --> 357 return _default_decoder.decode(s) 358 if cls is None: 359 cls = JSONDecoder File ~/opt/anaconda3/lib/python3.8/json/decoder.py:337, in JSONDecoder.decode(self, s, _w) 332 def decode(self, s, _w=WHITESPACE.match): 333 """Return the Python representation of ``s`` (a ``str`` instance 334 containing a JSON document). 335 336 """ --> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 338 end = _w(s, end).end() 339 if end != len(s): File ~/opt/anaconda3/lib/python3.8/json/decoder.py:355, in JSONDecoder.raw_decode(self, s, idx) 353 obj, end = self.scan_once(s, idx) 354 except StopIteration as err: --> 355 raise JSONDecodeError("Expecting value", s, err.value) from None 356 return obj, end JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Solution
If the file is empty, it is good practice to add a try-except statement to catch the JSONDecodeError. Let’s look at the code:
import json filename = 'particles.json' with open(filename, 'r') as f: try: contents = json.loads(f.read()) print(contents) except json.decoder.JSONDecodeError: print('File is empty')
File is empty
Now we have verified the file is empty, we can add some content to the file. We will add three particle names together with their masses.
[ {"proton":938.3}, {"neutron":939.6}, {"electron":0.51} ]
Let’s try to read the JSON file into our program and print the contents to the console:
import json filename = 'particles.json' with open(filename, 'r') as f: try: contents = json.loads(f.read()) print(contents) except json.decoder.JSONDecodeError: print('File is empty')
[{'proton': 938.3}, {'neutron': 939.6}, {'electron': 0.51}]
We successfully read the contents of the file into a list object.
Example #3: Response Request
Let’s look at an example where we want to parse a JSON response using the requests library. We will send a RESTful GET call to a server, and in return, we get a response in JSON format. The requests library has a built-in JSON decoder, response.json(), which provides the payload data in the JSON serialized format.
We may encounter a response that has an error status code or is not content-type application/json
. We must check that the response status code is 200 (OK) before performing the JSON parse. Let’s look at the code to check the response has the 200
status code and has the valid content type. application/json
.
import requests from requests.exceptions import HTTPError url = 'https://httpbin.org/get' try: response = requests.get(url) status = response.status_code if (status != 204 and response.headers["content-type"].strip().startswith("application/json")): try: json_response = response.json() print(json_response) except ValueError: print('Bad Data from Server. Response content is not valid JSON') elif (status != 204): try: print(response.text) except ValueError: print('Bad Data From Server. Reponse content is not valid text') except HTTPError as http_err: print(f'HTTP error occurred: {http_err}') except Exception as err: print(f'Other error occurred: {err}')
In the above example, we use httpbin.org to execute a GET call. Let’s run the code to get the result:
{'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, br', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.27.1', 'X-Amzn-Trace-Id': 'Root=1-6265a5c1-3b57327c02057a3a39ffe86d'}, 'origin': '90.206.95.191', 'url': 'https://httpbin.org/get'}
Summary
Congratulations on reading to the end of this tutorial! The JSONDecodeError: Expecting value: line 1 column 1 (char 0) occurs either when data is not present in a file, the JSON string is empty, or the payload from a RESTful call is empty.
You can resolve this error by checking the JSON file or string for valid content and using an if-statement when making a RESTful call to ensure the status code is 200 and the content type is application/json
.
For further reading on errors involving JSON, go to the articles:
- How to Solve Python ValueError: Trailing data
- How to Solve Python JSONDecodeError: extra data
- How to Solve Python TypeError: Object of type datetime is not JSON serializable
- How to Solve Python JSONDecodeError: Expecting ‘,’ delimiter: line 1
Have fun and happy researching!
Many developers store data from a program in a JSON file; other programs reference APIs which require working with JSON. Indeed, you will have no trouble finding a use case for JSON, or its Python equivalent, dictionaries.
You may encounter a JSONDecodeError when you are working with JSON data. In this guide, we’re going to talk about the causes of a JSONDecodeError and how to fix this error.
Find Your Bootcamp Match
- Career Karma matches you with top tech bootcamps
- Access exclusive scholarships and prep courses
Select your interest
First name
Last name
Phone number
By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.
Python JSONDecodeError
A Python JSONDecodeError indicates there is an issue with the way in which your JSON data is formatted. For example, your JSON data may be missing a curly bracket, or have a key that does not have a value, or be missing some other piece of syntax.
To completely fix a JSONDecodeError, you need to go into a JSON file to see what the problem is. If you anticipate multiple problems coming up in the future, you may want to use a try…except block to handle your JSONDecodeError.
Followed by the JSONDecodeError keyword, you should see a short description which describes the cause of the error.
All properly-formatted JSON should look like this:
“value” can be any valid JSON value, such as a list, a string, or another JSON object.
An Example Scenario
We are building a program that stores a list of JSON objects which represent which computers have been issued to employees at a business. Each JSON object should look like this:
[ { "name": "Employee Name", "equip_id": "000" } ]
We store these JSON objects in a file called equipment.json. The file only contains one entry:
[ { "name": "Laura Harper", "equip_id" "309" } ]
To read this data into our program, we can use the json module:
import json with open("equipment.json") as file: data = json.load(file) print("Equipment data has been successfully retrieved.")
First, we import the json module which we use to read a JSON file. Then, we use an open()
statement to read the contents of our JSON file. We print out a message to the console that tells us the equipment data has been retrieved once our with statement has run.
Let’s run our code and see what happens:
Traceback (most recent call last): File "<stdin>", line 2, in <module> File "/usr/lib/python3.8/json/__init__.py", line 293, in load return loads(fp.read(), File "/usr/lib/python3.8/json/__init__.py", line 357, in loads return _default_decoder.decode(s) File "/usr/lib/python3.8/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python3.8/json/decoder.py", line 353, in raw_decode obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting ':' delimiter: line 4 column 16 (char 47
Our code returns a long error. We can see Python describes the cause of our error after the term JSONDecodeError.
The Solution
Our JSONDecodeError is telling us we are missing a colon (:) in our JSON data. This colon should appear on line four at column 16. If we look at this line of data in our equipment.json file, we can see our JSON is invalid:
Our code is missing a colon. To fix this error, we should add a colon:
Now that we have fixed the problem with the way in which our data is represented, we can try to run our program again:
Equipment data has been successfully retrieved.
Our code executes successfully.
Alternatively, we could use a try…except handler to handle this issue so that our code will not immediately return an error if we face another formatting issue:
import json try: with open("equipment.json") as file: data = json.load(file) print("Equipment data has been successfully retrieved.") except json.decoder.JSONDecodeError: print("There was a problem accessing the equipment data.")
If there is an error in our JSON data, this program will return:
There was a problem accessing the equipment data.
Otherwise, the program will read the data and then display the following text on the console:
Equipment data has been successfully retrieved.
Conclusion
The Python JSONDecodeError indicates there is an issue with how a JSON object is formatted. To fix this error, you should read the error message and use it to guide you in fixing your JSON data. Alternatively, you can use a try…except block to catch and handle the error.
Are you interested in learning more about Python coding? Read our How to Learn Python guide. You’ll find expert advice on how to learn Python and a list of learning resources to help you build your knowledge.
When working with URLs and APIs in Pythons, you often have to use the urllib
and json
libraries. More importantly, the json
library helps with processing JSON data which is the default way to transfer data, especially with APIs.
Within the json
library, there is a method, loads()
, that returns the JSONDecodeError
error. In this article, we will discuss how to resolve such errors and deal with them appropriately.
Use try
to Solve raise JSONDecodeError("Expecting value", s, err.value) from None
in Python
Before dealing with JSON, we often had to receive data via the urllib
package. However, when working with the urllib package, it is important to understand how to import such a package into your code as it could result in errors.
For us to make use of the urllib
package, we have to import it. Often, people might import it as below.
import urllib
queryString = { 'name' : 'Jhon', 'age' : '18'}
urllib.parse.urlencode(queryString)
The output of the code above will give an AttributeError
:
Traceback (most recent call last):
File "c:UsersakinlDocumentsHTMLpythontest.py", line 3, in <module>
urllib.parse.urlencode(queryString)
AttributeError: module 'urllib' has no attribute 'parse'
The correct way to import the urllib
can be seen below.
import urllib.parse
queryString = {'name': 'Jhon', 'age': '18'}
urllib.parse.urlencode(queryString)
Alternatively, you can use the as
keyword and your alias (often shorter) to make it easier to write your Python code.
import urllib.parse as urlp
queryString = {'name': 'Jhon', 'age': '18'}
urlp.urlencode(queryString)
All of the above works for request
, error
, and robotparser
:
import urllib.request
import urllib.error
import urllib.robotparser
With these common errors out of the way, we can deal further with the function that often works with the urllib
library when working with URLs that throws the JSONDecodeError
error, as seen earlier, which is the json.load()
function.
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
The load()
method parses valid JSON string that it receives as an argument and converts it into a Python dictionary for manipulation. The error message shows that it expected a JSON value but did not receive one.
That means your code did not parse JSON string or parse an empty string to the load()
method. A quick code snippet can easily verify this.
import json
data = ""
js = json.loads(data)
The output of the code:
Traceback (most recent call last):
File "c:UsersakinlDocumentspythontexts.py", line 4, in <module>
js = json.loads(data)
File "C:Python310libjson__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:Python310libjsondecoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:Python310libjsondecoder.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)
The same error message is present, and we can ascertain that the error came from an empty string argument.
For a more detailed example, let us try to access a Google Map API and collect user location, e.g., US or NG, but it does not return any value.
import urllib.parse
import urllib.request
import json
googleURL = 'http://maps.googleapis.com/maps/api/geocode/json?'
while True:
address = input('Enter location: ')
if address == "exit":
break
if len(address) < 1:
break
url = googleURL + urllib.parse.urlencode({'sensor': 'false',
'address': address})
print('Retrieving', url)
uReq = urllib.request.urlopen(url)
data = uReq.read()
print('Returned', len(data), 'characters')
js = json.loads(data)
print(js)
The output of the code:
Traceback (most recent call last):
File "C:UsersakinlDocumentshtmlpythonjsonArt.py", line 18, in <module>
js = json.loads(str(data))
File "C:Python310libjson__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:Python310libjsondecoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:Python310libjsondecoder.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)
We obtained the same error. However, to catch such errors and prevent breakdown, we can use the try/except
logic to safeguard our code.
Therefore, if the API does not return any JSON value at a point of request, we can return another expression and not an error.
The above code becomes:
import urllib.parse
import urllib.request
import json
googleURL = 'http://maps.googleapis.com/maps/api/geocode/json?'
while True:
address = input('Enter location: ')
if address == "exit":
break
if len(address) < 1:
break
url = googleURL + urllib.parse.urlencode({'sensor': 'false',
'address': address})
print('Retrieving', url)
uReq = urllib.request.urlopen(url)
data = uReq.read()
print('Returned', len(data), 'characters')
try:
js = json.loads(str(data))
except:
print("no json returned")
The output of the code when we enter location as US
:
Enter location: US
Retrieving http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=US
Returned 237 characters
no json returned
Because no JSON value was returned, the code prints no json returned
. Because the error is more a non-presence of argument that cannot be controlled, the use of try/except
is important.