1

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))
Will
  • 24,082
  • 14
  • 97
  • 108
SHIVAM GOYAL
  • 71
  • 1
  • 2
  • 9
  • 2
    Works fine here. Are you sure you don't have another module named `json.py`? – Will Jul 09 '16 at 08:03
  • Actually there is no json.py.There is one json.pyc – SHIVAM GOYAL Jul 09 '16 at 10:22
  • Ah, yeah, remove that. Don't name modules after Python builtin modules :) – Will Jul 09 '16 at 10:24
  • That was inbuilt.I am using python 2.5.3 and json.pyc was already present in the 'LIB' folder where python is installed.Should I delete that and then run the code?There is no json.py file in 'LIB' – SHIVAM GOYAL Jul 09 '16 at 10:31
  • Oh, no, don't mess with libraries included with the Python distribution. I meant, was there a module named `json` in your code. But now I've found your problem and will answer shortly. – Will Jul 09 '16 at 10:33
  • Thanks for the help mate – SHIVAM GOYAL Jul 09 '16 at 16:35

1 Answers1

3

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 
Will
  • 24,082
  • 14
  • 97
  • 108
  • 1
    Thanks for the advice.I installed python 2.7 and it worked – SHIVAM GOYAL Jul 09 '16 at 16:35
  • No problem at all, glad to help! :) If this resolved your problem, please click the checkbox next to the answer to accept the answer and mark the question as resolved. – Will Jul 09 '16 at 18:08