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
arbi-g11324115
  • 193
  • 1
  • 2
  • 13

1 Answers1

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)
Totem
  • 7,189
  • 5
  • 39
  • 66
  • 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 – arbi-g11324115 Sep 27 '15 at 00:14
  • Yes, that's why I asked. – arbi-g11324115 Sep 27 '15 at 00:19
  • 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 – Totem Sep 27 '15 at 00:24
  • 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? – arbi-g11324115 Sep 27 '15 at 00:34
  • Just came across this https://stackoverflow.com/questions/8988775/convert-json-to-xml-in-python and this https://stackoverflow.com/questions/1019895/serialize-python-dictionary-to-xml – Totem Sep 27 '15 at 00:35
  • It shouldn't be too hard to find a way, so long as you remember that your loaded json is just a python dict. – Totem Sep 27 '15 at 00:38