Jeff S
Jeff S

Reputation: 1

Convert Json output into good looking text?

I am trying to retrieve data from the site: https://api.coinmarketcap.com/v1/ticker/cardano/?convert=usd

the code snippet looks like this:

with urllib.request.urlopen("https://api.coinmarketcap.com/v1/ticker/cardano/?convert=USD") as url:
data = json.loads(url.read().decode())
print(data)

The output is:

[{'id': 'cardano', 'name': 'Cardano', 'symbol': 'ADA', 'rank': '5', 'price_usd': '0.81872', 'price_btc': '0.00005809', '24h_volume_usd': '213316000.0', 'market_cap_usd': '21227011191.0', 'available_supply': '25927070538.0', 'total_supply': '31112483745.0', 'max_supply': '45000000000.0', 'percent_change_1h': '0.19', 'percent_change_24h': '13.13', 'percent_change_7d': '-19.93', 'last_updated': '1515768856'}]

My problem is, how do I work with that text? Can I make it into a better looking list?

Thanks in advance.

P.S.: I'm working with Python right now

Upvotes: 0

Views: 74

Answers (3)

Nico Villanueva
Nico Villanueva

Reputation: 904

I would very much recommend using the requests library for doing this kind of things. It's suuuuper flexible and is kind of the de-facto lib for doing requesty stuff.

For example (in which I took the liberty of using such lib, and iPython):

In [1]: import requests

In [2]: r = requests.get("https://api.coinmarketcap.com/v1/ticker/cardano/?convert=USD")

In [3]: r.status_code
Out[3]: 200

In [4]: r.json()
Out[4]:
[{'24h_volume_usd': '198429000.0',
  'available_supply': '25927070538.0',
  'id': 'cardano',
  'last_updated': '1515772155',
  'market_cap_usd': '20403048889.0',
  'max_supply': '45000000000.0',
  'name': 'Cardano',
  'percent_change_1h': '-3.09',
  'percent_change_24h': '5.94',
  'percent_change_7d': '-22.7',
  'price_btc': '0.00005650',
  'price_usd': '0.78694',
  'rank': '5',
  'symbol': 'ADA',
  'total_supply': '31112483745.0'}]

In [5]: usd = r.json()[0].get('price_usd')

In [6]: usd
Out[6]: '0.78694'

And if you wanted to print the response as a string, you can use the built-in lib json (for dumping it into a file or whatever):

In [8]: import json

In [10]: json.dumps(r.text, indent=2)
Out[10]: '"[\\n    {\\n        \\"id\\": \\"cardano\\", \\n        \\"name\\": \\"Cardano\\", \\n        \\"symbol\\": \\"ADA\\", \\n        \\"rank\\": \\"5\\", \\n        \\"price_usd\\": \\"0.78694\\", \\n        \\"price_btc\\": \\"0.00005650\\", \\n        \\"24h_volume_usd\\": \\"198429000.0\\", \\n        \\"market_cap_usd\\": \\"20403048889.0\\", \\n        \\"available_supply\\": \\"25927070538.0\\", \\n        \\"total_supply\\": \\"31112483745.0\\", \\n        \\"max_supply\\": \\"45000000000.0\\", \\n        \\"percent_change_1h\\": \\"-3.09\\", \\n        \\"percent_change_24h\\": \\"5.94\\", \\n        \\"percent_change_7d\\": \\"-22.7\\", \\n        \\"last_updated\\": \\"1515772155\\"\\n    }\\n]"'

Upvotes: -1

Joseph Mason
Joseph Mason

Reputation: 51

You could use something like:

print(json.dumps(data, indent=4))

This will break out into a much more easily readable view of your pull. But I'm not sure that it would make it any easier for you to work with the info within your script. pprint is another great module.

Upvotes: 1

Scott Hunter
Scott Hunter

Reputation: 49896

To get the price_usd element, you can use data[0]['price_usd'].

You can use the pprint module to print it in a nicer format.

Upvotes: 2

Related Questions