Just to make more clear the comment of @tim, in python3 you can just write
from json import JSONDecodeError
No need for simplejson package
Answer from George Bikas on Stack OverflowJust to make more clear the comment of @tim, in python3 you can just write
from json import JSONDecodeError
No need for simplejson package
You already have the answer on how to get JSONDecodeError, but I feel that the correct advice should be that you shouldn't try to import it.
The reason is that JSONDecodeError appears only in simplejson, and there's not really a reason to use that unless your Python version is severely outdated. The built-in json is just as fast in recent versions, and has no unicode bug. Info: https://stackoverflow.com/a/16131316/723090
The solution: json raises a ValueError instead of JSONDecodeError, but JSONDecodeError (raised by simplejson) is a subclass of ValueError. So you could simply except a ValueError and it'll work for json and simplejson!
ImportError: cannot import name 'JSONDecodeError'
JSONDecodeError after updating conda
python - Python3: unable to import JSONDecodeError from json.decoder - Stack Overflow
Bug: `JSONDecodeError` when trying to load JSON with an array at the top-level
Videos
According to 3.4.x docs, plain ValueError is raised when JSON decoding fails.
JSONDecodeError class is available starting from 3.5.x.
According to Docs from module json (Python version >= 3.5.0), Python which version < 3.5.0 does not support import statement like what you just did, but if you use Python(version>=3.5.0), your import statement is definitely correct.
JSON doc:
{
"Data" : [
"input1",
"input2",
"input3",
"input4"
]
}Python code:
import json
with open("data.json", 'r') as f:
json_data = json.load(f)
print(json_data)Error message:
sh-5.1$ /bin/python /home/USER/Documents/Python/learning
Traceback (most recent call last):
File "/home/USER/Documents/Python/learning", line 27, in <module>
json_data = json.load(f)
File "/usr/lib/python3.9/json/__init__.py", line 293, in load
return loads(fp.read(),
File "/usr/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.9/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)Disproved theories: Improper syntax (python), document not located, bad formatting (json), accidental empty space in json.
I get this error each time I try to use "json.load" so long as it has a valid parameter input. Whether read or write; error. I'm using Visual Studio Code on Linux Mint in case that matters.
Thanks for your time! I'll be happy to answer questions in the morning when I wake up.
EDIT: Clarity, and reddit dislikes my use of backticks. Manually tabbed code.
EDIT: This code SHOULD work. It does on other computers. But it doesn't on mine, and I don't know why nor how to fix it.
There is a rule in Python programming called "it is Easier to Ask for Forgiveness than for Permission" (in short: EAFP). It means that you should catch exceptions instead of checking values for validity.
Thus, try the following:
try:
qByUser = byUsrUrlObj.read()
qUserData = json.loads(qByUser).decode('utf-8')
questionSubjs = qUserData["all"]["questions"]
except ValueError: # includes simplejson.decoder.JSONDecodeError
print('Decoding JSON has failed')
EDIT: Since simplejson.decoder.JSONDecodeError actually inherits from ValueError (proof here), I simplified the catch statement by just using ValueError.
If you don't mind importing the json module, then the best way to handle it is through json.JSONDecodeError (or json.decoder.JSONDecodeError as they are the same) as using default errors like ValueError could catch also other exceptions not necessarily connected to the json decode one.
from json.decoder import JSONDecodeError
try:
qByUser = byUsrUrlObj.read()
qUserData = json.loads(qByUser).decode('utf-8')
questionSubjs = qUserData["all"]["questions"]
except JSONDecodeError as e:
# do whatever you want
//EDIT (Oct 2020):
As @Jacob Lee noted in the comment, there could be the basic common TypeError raised when the JSON object is not a str, bytes, or bytearray. Your question is about JSONDecodeError, but still it is worth mentioning here as a note; to handle also this situation, but differentiate between different issues, the following could be used:
from json.decoder import JSONDecodeError
try:
qByUser = byUsrUrlObj.read()
qUserData = json.loads(qByUser).decode('utf-8')
questionSubjs = qUserData["all"]["questions"]
except JSONDecodeError as e:
# do whatever you want
except TypeError as e:
# do whatever you want in this case
I took over a project that had its own CI pipeline already set up. This pipeline has been working fine for the last few weeks until today.
When running eb deploy --staged my-branch-name I get the following error:
Traceback (most recent call last):
File "/usr/local/bin/eb", line 5, in <module>
from ebcli.core.ebcore import main
File "/usr/local/lib/python2.7/dist-packages/ebcli/core/ebcore.py", line 19, in <module>
from ebcli.core import ebglobals, base, hooks
File "/usr/local/lib/python2.7/dist-packages/ebcli/core/hooks.py", line 20, in <module>
from ebcli.core import fileoperations
File "/usr/local/lib/python2.7/dist-packages/ebcli/core/fileoperations.py", line 32, in <module>
from json import load, JSONDecodeError
ImportError: cannot import name JSONDecodeError I'm somewhat unsure of what to do here. I haven't changed anything relating to CI or any of the AWS/EB configuration files.
The pipeline uses python 2.7 which according to the ebcli docs is still supported. I've tried rerunning a few times now but always get the same result. The last successful deploy with this pipeline was 3 days ago, on Wednesday.
Has anyone seen this error before or have any suggestions on how I could go about debugging it?
EDIT: It seems the awsebcli had an update the day after my last successful deploy. The changelogs don't hint at anything that would break my setup, but I think this may be the cause.
EDIT2: Locking the awsebcli to the previous version has fixed it for now. Hopefully i can get budget to slot in an upgrade to the pipeline and fix this issue once and for all. Thanks for the help everyone who commented, it's much appreciated as always.