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


Groups > comp.lang.python > #54115

Re: Another question about JSON

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'api.': 0.05; 'argument': 0.05; 'string.': 0.05; 'json': 0.07; 'see:': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'valueerror:': 0.09; 'subject:question': 0.10; 'python': 0.11; '(it': 0.16; '(just': 0.16; '``s``': 0.16; '``str``': 0.16; '``unicode``': 0.16; 'document)': 0.16; 'expects': 0.16; 'json,': 0.16; 'json:': 0.16; 'obj,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'skip:{ 40': 0.16; 'url.': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'bit': 0.19; 'module': 0.19; 'trying': 0.19; 'everyone,': 0.19; 'replacing': 0.19; 'seems': 0.21; '>>>': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; '31,': 0.24; "i've": 0.25; 'skip:" 30': 0.26; 'code:': 0.26; 'skip:_ 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'raise': 0.29; 'returned': 0.30; "i'm": 0.30; "skip:' 10": 0.31; 'loads': 0.31; 'object.': 0.31; 'anyone': 0.31; 'file': 0.32; 'figure': 0.32; '(most': 0.33; "i'd": 0.34; 'could': 0.34; 'skip:u 20': 0.35; 'something': 0.35; 'good.': 0.35; 'acceptable': 0.36; 'skip:j 20': 0.36; 'done': 0.36; 'doing': 0.36; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'simply': 0.61; 'first': 0.61; 'containing': 0.69; 'default': 0.69; '366,': 0.84; 'exercise.': 0.84; 'url:v1': 0.84; 'browser:': 0.91; 'trouble.': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Another question about JSON
Date Fri, 13 Sep 2013 15:24:25 +0200
Organization None
References <52330C7F.7070203@gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084a114.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
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.356.1379078674.5461.python-list@python.org> (permalink)
Lines 65
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1379078674 news.xs4all.nl 15916 [2001:888:2000:d::a6]:46045
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:54115

Show key headers only | View raw


Anthony Papillion wrote:

> Hello Again Everyone,
> 
> I'm still working to get my head around JSON and I thought I'd done so
> until I ran into this bit of trouble. I'm trying to work with the
> CoinBase API. If I type this into my browser:
> 
> https://coinbase.com/api/v1/prices/buy
> 
> I get the following JSON returned
> 
> {"subtotal":{"amount":"128.00","currency":"USD"},"fees":[{"coinbase":
{"amount":"1.28","currency":"USD"}},{"bank":
{"amount":"0.15","currency":"USD"}}],"total":
{"amount":"129.43","currency":"USD"},"amount":"129.43","currency":"USD"}
> 
> So far, so good. Now, I want to simply print out that bit of JSON (just
> to know I've got it) and I try to use the following code:
> 
> returnedJSON = json.loads('https://coinbase.com/api/v1/prices/buy')
> print returnedString
> 
> 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
> 
> 
> I'm very confused since the URL is obviously returned a JSON string. Can
> anyone help me figure this out? What am I doing wrong?

Let's see:

>>> help(json.loads)
Help on function loads in module json:

loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, 
parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
    Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON
    document) to a Python object.
[...]

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.

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


Thread

Re: Another question about JSON Peter Otten <__peter__@web.de> - 2013-09-13 15:24 +0200

csiph-web