Python JSON module has no attribute 'dumps'

Alex Reynolds picture Alex Reynolds · Jul 6, 2012 · Viewed 57.1k times · Source

I am running Python 2.7 (x64 Linux) and trying to convert a dict to a JSON object.

>>> import sys
>>> sys.version_info
sys.version_info(major=2, minor=7, micro=0, releaselevel='final', serial=0)

I am trying to use simplejson (falling back to json from the standard library) but I get the following error:

>>> try: import simplejson as json
... except ImportError: import json
...                  
>>> metadata = dict()
>>> metadata['foo'] = 'bar'
>>> print metadata
{'foo': 'bar'}
>>> json.dumps(metadata)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'dumps' 

Is there something obvious I am missing about using json or simplejson with Python 2.7?

Answer

Paulo SantAnna picture Paulo SantAnna · Nov 28, 2012

Had a similar issues, it was caused by another custom module. I named another script json.py and it turns out it tried to load the custom json.py file as a module. dumps method is obviously not available there.

Renaming the json.py script to something else (json2.py) got rid of the issue.