Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
major and annoying differences between module versions of Pythons JSON Library
Two versions of the same library behave completely different :-/
the PI
```
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> try:
... json.loads('notjson')
... except json.JSONDecodeError:
... print("didn't work")
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
AttributeError: 'module' object has no attribute 'JSONDecodeError'
>>>
```
the Mac:
```
Python 3.5.0 (default, Sep 23 2015, 04:41:38)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> try:
... json.loads("notjson")
... except json.JSONDecodeError:
... print("didn't work")
...
didn't work
>>>
```
@c7h

This comment has been minimized.

Copy link
Owner Author

commented Jul 17, 2017

JSONDecodeError was introduced in 3.5
https://docs.python.org/3/whatsnew/3.5.html#json

Use ValueError in older versions instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.