0

I am attempting to parse data in JSON format from YQL, but it won't work. Specifically, I am getting the following output:

 File "C:\Python34\lib\json\__init__.py", line 318, in loads
    return _default_decoder.decode(s)
 File "C:\Python34\lib\json\decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s,0).end())
 File "C:\Python34\lib\json\decoder.py", line 361, in raw_decode
    raise ValueError(errmsg("Expecting value", s, err.value)) from None
 ValueError: Expecting Value: line 1 column 1 (char 0)

This is my first attempt at doing anything with Python. I work primarily in C++ and VBA, but this is for research that I'm doing at school. I've also looked at other questions that are similar on SO, but those were parsing from .txt files, not straight from the internet. Any insight anyone could give would be greatly appreciated--thanks!

Here is my code:

import json
result = json.loads('http://query.yahooapis.com/v1/public/yql?q=select%20Ask%20from%20yahoo.finance.quotes%20where%20symbol%20IN%20(%22CL=F%22)&format=json&env=http://datatables.org/alltables.env')  # result is now a dict
print (result['Ask'])

3 Answers 3

3

You have to fetch the data before you can decode it. Try using the requests module to fetch the data.

import requests

url = 'http://query.yahooapis.com/v1/public/yql'
params = {
    'q': 'select Ask from yahoo.finance.quotes where symbol IN ("CL=F")',
    'format': 'json',
    'env': 'http://datatables.org/alltables.env'
}

response = requests.get(url, params=params)
data = response.json()
print (data)
Sign up to request clarification or add additional context in comments.

Comments

1
import json, requests

url = 'http://query.yahooapis.com/v1/public/yql?q=select%20Ask%20from%20yahoo.finance.quotes%20where%20symbol%20IN%20(%22CL=F%22)&format=json&env=http://datatables.org/alltables.env'


resp = requests.get(url=url)
data = json.loads(resp.text)

Comments

0

json.loads converts the string passed into a dict object. It does not make a URL call and then parse the result.

You need to use either urllib2

>>> import urllib2
>>> content = urllib2.urlopen(some_url).read()
>>> print json.loads(content)

Or the requests library

>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
>>> print r.json()

urllib2 or requests will fetch the JSON data by sending a request to the URL. Once you receive the data then you can parse it using json.loads().

Comments

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.