4

Using HTTP requests in Python I was able to get a response, however neither json nor simplejson modules are able to unpackage it. Both claim that the input is not good json.

import requests
import json

html_base = u"http://www.google.com/trends/fetchComponent?q="
q = u"asdf,qwerty"
query_type = u"&cid=TIMESERIES_GRAPH_0&export=3"
full_query = html_base + q + query_type

response = requests.get(full_query)
data = json.loads(response.text)

The error:

C:\Anaconda\lib\json\decoder.pyc in raw_decode(self, s, idx)
    382             obj, end = self.scan_once(s, idx)
    383         except StopIteration:
--> 384             raise ValueError("No JSON object could be decoded")
    385         return obj, end

ValueError: No JSON object could be decoded
ljk321
  • 16,242
  • 7
  • 48
  • 60
billmanH
  • 1,298
  • 1
  • 14
  • 25

2 Answers2

3

Please excuse my necromancing but, here is a clean work around for any who should stumble this way in the future.

import ast

nice_dict = ast.literal_eval(response.text.split('setResponse(')[1].rstrip()[:-2].replace('new Date', ''))

ast.literal_eval() takes a string and returns a dict. Everything on the inside cleans up the json to make it returnable. Note, your dates are now tuples.

rawkintrevo
  • 659
  • 5
  • 16
2

That's because it's just not JSON. If you put the URL in your browser and look at the output, you'll see it's actually JavaScript. It starts with a comment, then a function call - and the JSON itself is inside the function call.

Ben
  • 6,687
  • 2
  • 33
  • 46
  • That's what I thought. Is there an easy way to parse the JS into good JSON without substring manipulation? – billmanH Oct 06 '14 at 02:25
  • Unfortunately, not easily, particularly because inside the JSON are other calls to JavaScript functions such as `newDate`. – Ben Oct 06 '14 at 07:27