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


Groups > comp.lang.python > #54114 > unrolled thread

Another question about JSON

Started byAnthony Papillion <papillion@gmail.com>
First post2013-09-13 08:00 -0500
Last post2013-09-13 14:35 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Another question about JSON Anthony Papillion <papillion@gmail.com> - 2013-09-13 08:00 -0500
    Re: Another question about JSON John Gordon <gordon@panix.com> - 2013-09-13 14:35 +0000

#54114 — Another question about JSON

FromAnthony Papillion <papillion@gmail.com>
Date2013-09-13 08:00 -0500
SubjectAnother question about JSON
Message-ID<mailman.355.1379077258.5461.python-list@python.org>
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?

Thanks in advance!
Anthony


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

[toc] | [next] | [standalone]


#54118

FromJohn Gordon <gordon@panix.com>
Date2013-09-13 14:35 +0000
Message-ID<l0v7qk$foa$1@reader1.panix.com>
In reply to#54114
In <mailman.355.1379077258.5461.python-list@python.org> Anthony Papillion <papillion@gmail.com> writes:

> 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

JSON is a notation for exchanging data; it knows nothing about URLs.

It's up to you to connect to the URL and read the data into a string,
and then pass that string to json.loads().

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web