1

I am fetching an API from url like: http://api.example.com/search/foo/bar

using this simple code.

import json
url = "http://api.example.com/search/foo/bar"
result = json.loads(url)  # result is now a dict
print result['name']

But, I am getting this error.

Traceback (most recent call last):
  File "index.py", line 6, in <module>
    result = json.loads(url);
  File "/usr/lib64/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.7/json/decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python2.7/json/decoder.py", line 383, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
3
  • "http://api.example.com/search/foo/bar" is a string, did you want to download the page? Commented Sep 26, 2015 at 23:59
  • @KevinGuan No, not at all. It is a JSON response, it's not a string. Commented Sep 27, 2015 at 0:02
  • @arbi-g11324115 the URL is a string. You must download the page first to get its json content. That's what Kevin is telling you. He's right. Commented Sep 27, 2015 at 0:19

1 Answer 1

5

You need to read the data from the url first. json.loads() loads the json from a string. But that string is essentially just the json structure in string form. You need to get that json string by reading the data from that url request, which should be the json string.

For instance, something like this:

import json
import urllib2
url = "http://api.example.com/search/foo/bar"
response = urllib2.urlopen(url)
json_string = response.read()

json_string now contains the json you seek, assuming that the api call returns it correctly.

json_dict = json.loads(json_string)

You should be able to access the items in the json with json_dict['name'] etc.

json.loads() loads the json from a string, which is what is done above(and why I used read() to get the string). json.load() loads from a json object. If the api is returning a pure json format as you mentioned in the comments, you could try this instead:

response = urllib2.urlopen(url)
json_dict = json.load(response)
Sign up to request clarification or add additional context in comments.

6 Comments

thanks for the answer. I am new and I didn't understand most of it. Why is urllib2 library required? The returned data is pure JSON format, so why urllib2 urllib2.urlopen(url) or even html.read are required is beyond me
Yes, that's why I asked.
The urllib2.urlopen(url) returns a file-like object on the url you pass to it. calling read() on that object is required to, in this case, assign that file objects data into a variable as a string. As to exactly why it needs to be done this way, I'm not entirely sure. See my edit in any case. Sorry I can't shed more light on the why :s
Thanks. I'm actually polling the results and trying to export them to XML. Is there anyway, to just dump the entire JSON to XML?
|

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.