CoderFromHell
Power Member
- Mar 19, 2019
- 727
- 341
Can any one reference a good tutorial on using Python to work with API or Python using JSON?
import json
import requests
# download raw json object
url = "https://api.gdax.com/products/BTC-EUR/ticker"
#data = urllib.request.urlopen(url).read().decode()
data = requests.get(url)
# parse json object
obj = json.loads(data.text)
# output some object attributes
print('$ ' + obj['price'])
print('$ ' + obj['volume'])
$ 9153.25
$ 1025.52404485
Requests library also allows direct access to JSON object.Have a look at https://www.geeksforgeeks.org/working-with-json-data-in-python/
And using https://pythonbasics.org/json/ ( i adjusted it slightly to play with it in https://www.programiz.com/python-programming/online-compiler/ ) with https://api.gdax.com/products/BTC-EUR/ticker
Python:import json import requests # download raw json object url = "https://api.gdax.com/products/BTC-EUR/ticker" #data = urllib.request.urlopen(url).read().decode() data = requests.get(url) # parse json object obj = json.loads(data.text) # output some object attributes print('$ ' + obj['price']) print('$ ' + obj['volume'])
gives
Code:$ 9153.25 $ 1025.52404485
Now this is a straight forward JSON. You ll see some that are "nested" and takes a little bit more to get working with these .
Always remember to "dump" your output using the print function , so you can see where you are going right/wrong.
List & Dictionary are 2 things you should figure out - Im still getting confused .
Check out - https://stackoverflow.com/questions/23306653/python-accessing-nested-json-data/23337192 youll see what I mean
Complex - https://hackersandslackers.com/extract-data-from-complex-json-python/
Hope some of this helps.