1

I am trying to parse JSON data from a response:

If use my browswer to go to: /url/gold/stuff/here

I get a response in the browser as:

window.data_gold = [{'gold':' room 123' } , {'better gold':'room 1234'} , {"best gold": "in cellar"}];

How can I extract the JSON data out of the response

window.data_gold = json 

My code:

import requests,json

url     = '/url/gold/stuff/here'
r       = requests.get(url,timeout = 30)
newJSON = r.text

The above returns all the text, .json does not work.

3
  • 1
    What you got is not json. It's javascript. Commented Apr 30, 2014 at 2:00
  • @Leonardo Thats just semantics, how do I parse this. Commented Apr 30, 2014 at 2:03
  • It's actually an important distinction @Leonardo.Z made; you have to convert the Javascript into valid JSON, which may or may not be a viable long-term option. Commented Apr 30, 2014 at 2:10

2 Answers 2

3

Try something like

data = json.loads(r.text[19:-1])

Edit: it doesn't like that, but this works:

import ast

data = ast.literal_eval(r.text[19:-1])

which gives

[{'gold': ' room 123'},
 {'better gold': 'room 1234'},
 {'best gold': 'in cellar'}]
Sign up to request clarification or add additional context in comments.

2 Comments

good to see you again. The '19' should be all characters up to the square backet?
Yup, and :-1 to trim the ";" off the end.
3

Here's a hideous looking one-liner that will pull the json out of that particular response and put it in a dict.

d = json.loads(r.text.split("=")[1].replace("'", '"')[:-1])

It's pulling the json itself out of the javascript statement, replacing the single-quotes with double-quotes (because the json module won't accept single-quotes), and then feeding it the json module.

Edit As pointed out by Hugh Bothwell, using ast.literal_eval instead of the json module avoids the single-quote issue, so you're left with

d = ast.literal_eval(r.text.split("=")[1].strip(" ;"))

The .strip(" ;") Will strip the ';' off the end and the whitespace from the beginning.

1 Comment

(because the json module won't accept single-quotes) That's because JSON won't accept single-quotes as delimiters :)

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.