Skip to content

Instantly share code, notes, and snippets.

@tomplex
Created July 9, 2016 17:48
Show Gist options
  • Save tomplex/775af19ae9f7e785ced65f014fe73cf5 to your computer and use it in GitHub Desktop.
Save tomplex/775af19ae9f7e785ced65f014fe73cf5 to your computer and use it in GitHub Desktop.
Convert jquery response to json
import requests
import json
def convert_to_json(url):
# get the response
r = requests.get(url)
# get the content of the response and decode from bytes to str in UTF-8
response = r.content.decode()
# the response is a jquery object, so we need to strip off the extra jquery nonsense.
# the beginning of the json object is the '[' so...
while not response.startswith('['):
# while the first character isn't a [, remove it
response = response[1:]
# now remove the final parenthesis
response = response[:-1]
return json.loads(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment