Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54121

Re: Another question about JSON

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <papillion@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'argument': 0.05; 'json': 0.07; 'pgp': 0.09; 'req': 0.09; 'resp': 0.09; 'valueerror:': 0.09; 'subject:question': 0.10; '(it': 0.16; 'expects': 0.16; 'fingerprint:': 0.16; 'json,': 0.16; 'obj,': 0.16; 'url.': 0.16; 'wrote:': 0.18; 'replacing': 0.19; 'seems': 0.21; 'header:User- Agent:1': 0.23; '31,': 0.24; 'help!': 0.26; 'skip:" 30': 0.26; 'skip:_ 20': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'raise': 0.29; 'needed.': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'loads': 0.31; 'work:': 0.31; 'file': 0.32; '(most': 0.33; 'could': 0.34; 'skip:u 20': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'acceptable': 0.36; 'skip:j 20': 0.36; 'message-id:@gmail.com': 0.38; 'thank': 0.38; 'to:addr :python-list': 0.38; 'rather': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'first': 0.61; 'skip:r 40': 0.68; 'default': 0.69; '366,': 0.84; 'exercise.': 0.84; 'otten': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=1ZqiMCpvTtongSpJTEF46AsQ+YphCCvN1RY4D02oLh0=; b=Gt7dtdkqdlXIWM0yPq5j7F1hljhWgru/xgZ0YM8auPS8qbkXPsYqICCi1PwwTDg6/D zmCYi6RLQ4jXrMPK3So4QGatOkqKuxkNkmKmqZ5kgmhK37t1sX1i7RVU653SQmLVAWo0 6bdUuUkRwjCouQP2+CLfIFxb0IsCuvAK7zJuD0ZwyZCkdxihJZRta3bBD8qVyYeN2q6z /0aPBNx/OKT8U3AxQ8LNBisCfkUcja5Z15IOaCkJH5lKPWuP3xMWUlsrbG/eiDF1qPND ufqCXsvmXKfK+f8vFNrk9vMcr/REfNTXBaawayflNW+DmCuziSEkOVn0vUcCRPcK9chj eMVQ==
X-Received by 10.60.94.164 with SMTP id dd4mr2245633oeb.68.1379084726187; Fri, 13 Sep 2013 08:05:26 -0700 (PDT)
Date Fri, 13 Sep 2013 10:05:24 -0500
From Anthony Papillion <papillion@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130804 Thunderbird/17.0.8
MIME-Version 1.0
To python-list@python.org
Subject Re: Another question about JSON
References <52330C7F.7070203@gmail.com> <l0v3lo$57c$1@ger.gmane.org>
In-Reply-To <l0v3lo$57c$1@ger.gmane.org>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.357.1379084729.5461.python-list@python.org> (permalink)
Lines 55
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1379084729 news.xs4all.nl 15920 [2001:888:2000:d::a6]:47270
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:54121

Show key headers only | View raw


On 09/13/2013 08:24 AM, Peter Otten wrote:
> Anthony Papillion wrote:
> 
>> And I get a traceback that says: No JSON object could be decoded. The
>> specific traceback is:
>>
>> Traceback (most recent call last):
>>   File "coinbase_bot.py", line 31, in <module>
>>     getCurrentBitcoinPrice()
>>   File "coinbase_bot.py", line 28, in getCurrentBitcoinPrice
>>     returnedString = json.loads(BASE_API_URL + '/prices/buy')
>>   File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
>>     return _default_decoder.decode(s)
>>   File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
>>     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
>>   File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
>>     raise ValueError("No JSON object could be decoded")
>> ValueError: No JSON object could be decoded
> 
> So json.loads() expects its first argument to b valid json, no a URL.
> You have to retrieve the data using other means before you can deserialize 
> it:
> 
> data = urllib2.urlopen(...).read()
> returned_json = json.loads(data)
> 
> Replacing ... with something that works is left as an exercise. (It seems 
> that you have to use a Request object rather than a URL, and that the 
> default "Python-urllib/2.7" is not an acceptable user agent.

Thank you Peter! That was all I needed. So here's the code I came up
with that seems to work:

req = urllib2.Request(BASE_URL + '/prices/buy')
req.add_unredirected_header('User-Agent', USER_AGENT)
resp = urllib2.urlopen(req).read()
data - json.loads(resp)
return data['amount']

Thank you for the help!

Anthony






-- 
Anthony Papillion
XMPP/Jabber:      cypherpunk@patts.us
OTR Fingerprint:  4F5CE6C07F5DCE4A2569B72606E5C00A21DA24FA
SIP:              17772471988@callcentric.com
PGP Key:          0xE1608145

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Another question about JSON Anthony Papillion <papillion@gmail.com> - 2013-09-13 10:05 -0500

csiph-web