0

I'm trying to parse JSON using the json library. I'm executing the chunk of code below, and I get the error:

Traceback (most recent call last):
  File "test1.py", line 12, in <module>
    parsedResponse = json.loads(data)
  File "/usr/local/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python2.7/json/decoder.py", line 360, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python2.7/json/decoder.py", line 378, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

The code is:

import urllib, urllib2
from django.utils import simplejson
import json

opener = urllib2.build_opener()
requestURL = "http://api.shopstyle.com/action/apiSearch?pid=2254&fts=red+dress&min=0&count=10"
data = opener.open(requestURL).read().decode('utf8')
print data #this works
parsedResponse = json.loads(data)

I tried removing the read().decode('utf8') and passing that into json.load(), but that doesn't work either. I'd appreciate any help :)

Thanks.

4
  • 1
    What is the output of print data? It sounds like what you're receiving is not valid, or not properly formed. Commented Sep 26, 2011 at 2:14
  • Can I attach the output in this post somehow? It's a pretty long output :) Commented Sep 26, 2011 at 2:19
  • 1
    Debugging 101: print repr(data) (don't do just print data) then copy/paste it into an edit of your question. Commented Sep 26, 2011 at 2:21
  • @iman453: (1) use repr()!!! (2) the first 100 bytes or so should be enough Commented Sep 26, 2011 at 2:22

2 Answers 2

4

When you printed your output, did it by any chance look like this?

<SearchResult>
<QueryDetails>
<Category>womens-clothes</Category>
<CategoryName>Clothing</CategoryName>
<ShowSizeFilter>false</ShowSizeFilter>
<ShowColorFilter>true</ShowColorFilter>
...

That's XML, not JSON.

Sign up to request clarification or add additional context in comments.

4 Comments

Oh it did. The documentation said it would return JSON, and I'm new to web programming so I kinda assumed it was JSON. Thanks :)
Some quick Googling seems to indicate that just appending &format=json2 to that URL should give you JSON, if that's the format you want. This is the URL you had in your snippet, but in JSON format.
Thanks John! I appreciate your help (and also feel a little stupid for posting this...should have researched more heh)
+1 For the excellent comment, rather than the obvious answer :)
1

is data null? is data not proper JSON?

from your URL it seems that its outputting XML not JSON tho.

if you can post the output of data that would help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.