SHIVAM GOYAL
SHIVAM GOYAL

Reputation: 81

AttributeError: 'module' object has no attribute 'loads' while parsing json in python

I am getting this error:

Traceback (most recent call last):
File "C:/Users/Shivam/Desktop/jsparse.py", line 13, in <module>
info = json.loads(str(data))
AttributeError: 'module' object has no attribute 'loads'

Any thoughts what wrong I am doing here?

This is my code:

import json
import urllib
url = ''
uh = urllib.urlopen(url)
data = uh.read()
info = json.loads(str(data))

Upvotes: 1

Views: 6288

Answers (1)

Will
Will

Reputation: 24699

The problem is that you're using Python 2.5.x, which doesn't have the json module. If possible, I recommend upgrading to Python 2.7.x, as 2.5.x is badly outdated.

If you need to stick with Python 2.5.x, you'll have to use the simplejson module (see here). This code will work for 2.5.x as well as newer Python versions:

try:
    import json
except ImportError:
    import simplejson as json 

Or if you're only using Python 2.5, just do:

import simplejson as json 

Upvotes: 3

Related Questions