user4199057
user4199057

Reputation: 47

Python Json Error: ValueError: No JSON object could be decoded

I'm getting an error when I execute this script and can't figure it out.

The Error:

Traceback (most recent call last):
  File "./upload.py", line 227, in <module>
    postImage()
  File "./upload.py", line 152, in postImage
    reddit = RedditConnection(redditUsername, redditPassword)
  File "./upload.py", line 68, in __init__
    self.modhash = r.json()['json']['data']['modhash']
  File "/usr/lib/python2.6/site-packages/requests/models.py", line 799, in json
    return json.loads(self.text, **kwargs)
  File "/usr/lib/python2.6/site-packages/simplejson/__init__.py", line 307, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.6/site-packages/simplejson/decoder.py", line 335, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.6/site-packages/simplejson/decoder.py", line 353, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Upvotes: 3

Views: 7562

Answers (1)

Tay
Tay

Reputation: 298

You are getting this exception because you are using the wrong json function here:

def getNumberOfFailures(path):
try:
    with open(path + '.failurecount') as f:
        return json.loads(f.read())
except:
    return 0

You need to do this instead:

def getNumberOfFailures(path):
    try:
        with open(path + '.failurecount') as f:
            return json.load(f)
    except:
        return 0

json.loads() is used on json strings. json.load() is used on json file objects.

As some people have mentioned, you need to reissue yourself a new API key and delete the one you posted here in your code. Other people can and will abuse those secret keys to spam Reddit under your name.

Upvotes: 5

Related Questions