Python: Working with API & JSON Turtorial?

CoderFromHell

Power Member
Joined
Mar 19, 2019
Messages
727
Reaction score
341
Can any one reference a good tutorial on using Python to work with API or Python using JSON?
 
each api usually has a library for each language, that might help. otherwise you just need to learn how to send http requests and then import the json into a python array.
 
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.
 
I recommend checking out "Programming with mosh" on YouTube. The guy is pretty awesome. I ended up buying his react and react native courses as it landed me good projects. He also has tutorials on python as far as I can remember.
 
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.
Requests library also allows direct access to JSON object.

Instead of this:
data = requests.get(url)
obj = json.loads(data.text)

You can use:
obj = requests.get(url).json()

Answer for OP:
Working with JSON in Python is simple. If you'll get data from api endpoint, you can use requests library. It's really simple to use.
You can look to offical document: https://requests.readthedocs.io/en/master/
 
Back
Top