7

I setup virtualenv using python 3.4.3 and tried to import JSONDecodeError from json.decoder

from json.decoder import JSONDecodeError (I think its valid in python3 ? But why not working for me ?) 

But it is not working. Only below is working:

from simplejson import JSONDecodeError

How I did ?

virtualenv venv --no-site-packages -p python3 
pip install ipython
ipython
from json.decoder import JSONDecodeError
ImportError: cannot import name 'JSONDecodeError'

enter image description here

5
  • Is your file named json.py? Please provide full traceback. Commented Jun 23, 2017 at 6:01
  • from json import JSONDecodeError Commented Jun 23, 2017 at 6:06
  • Strange, this works fine for me. Can you see what happens if you try from json import decoder; print(decoder.JSONDecodeError) ? Commented Jun 23, 2017 at 6:06
  • In [8]: print(decoder.JSONDecodeError) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-8-bb2acb16c5bc> in <module>() ----> 1 print(decoder.JSONDecodeError) AttributeError: 'module' object has no attribute 'JSONDecodeError' Commented Jun 23, 2017 at 6:07
  • Attached screenshot also Commented Jun 23, 2017 at 6:11

3 Answers 3

21

According to 3.4.x docs, plain ValueError is raised when JSON decoding fails.

JSONDecodeError class is available starting from 3.5.x.

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed. And since JSONDecodeError inherits from ValueError it is best to catch the latter if you care about portability.
2

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.

Comments

0

json is the version of simplejson that has been integrated into Python. They have since been developed separately and are not the same anymore. So they cannot necessarily be used interchangably.

See this answer for more details about the differences.

2 Comments

Can you clarify whether 'from json import JSONDecodeError' is right or wrong in python3 ?
As mentioned in other answers it depends on the minor version. If your questioned isn't "Why does this work with simplejson but not with json?" then I misunderstood.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.