Problem Formulation
Say, you have a given URL stored as a string in your Python script. You know that if you’d call get url
, the server returns a JSON object.
How to get the JSON object from a given URL in Python?
Solution with urllib and json Modules
You can get the JSON object from a given URL string in three steps.
- Import the modules
urllib.request
andjson
. - Open the connection to the server in a
with
environment by runningwith urllib.request.urlopen(your_url) as url:
- Load the data from the server via
json.loads(url.read().decode())
and store the resulting dictionary in your data variable.
The following code loads all transaction data into the first “Genesis” block of the Bitcoin blockchain from the URL 'https://blockchain.info/rawaddr/12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX'
.
import urllib.request import json # Bitcoin Genesis Block Transactions your_url = 'https://blockchain.info/rawaddr/12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX' with urllib.request.urlopen(your_url) as url: data = json.loads(url.read().decode()) print(data)
The output is the JSON data as a dictionary:
{'hash160': '119b098e2e980a229e139a9ed01a469e518e6f26', 'address': '12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX', 'n_tx': 124, 'n_unredeemed ...
You can try this yourself in our interactive Jupyter notebook with Google Colab:
An even simpler way to read a JSON object from a given URL is provided by the pandas library.
Shorter Solution with Pandas
You can extract a JSON object from a given URL by using the pandas.read_url('your_url')
method by replacing the string with your specific URL. The result of this operation is a pandas DataFrame that you can use for further processing or analysis.
import urllib.request import json import pandas your_btc_address = '12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX' # Genesis Block transactions_url = 'https://blockchain.info/rawaddr/' + your_btc_address print(pandas.read_json(transactions_url))
The output is even more beautifully structured:
hash160 ... txs 0 119b098e2e980a229e139a9ed01a469e518e6f26 ... {'hash': '367c89950f2865a2c07feb84def6d9fb061b... 1 119b098e2e980a229e139a9ed01a469e518e6f26 ... {'hash': 'e585ec92476a3e867eb991ec1115788ef684... 2 119b098e2e980a229e139a9ed01a469e518e6f26 ... {'hash': '8241cb1f6a48879fb712b398c6d0e1bba88a... 3 119b098e2e980a229e139a9ed01a469e518e6f26 ... {'hash': 'a0b98914bb4ce5660e1ce3bafd268a0b159a... 4 119b098e2e980a229e139a9ed01a469e518e6f26 ... {'hash': '640b727abd8605031e86fc2a2fa1fe41d607... .. ... ... ... 95 119b098e2e980a229e139a9ed01a469e518e6f26 ... {'hash': '9fefa807ab8791b6a4fca5f1d8de1b5fd012... 96 119b098e2e980a229e139a9ed01a469e518e6f26 ... {'hash': '56484b549f42a4485fb79b2838c7829805d0... 97 119b098e2e980a229e139a9ed01a469e518e6f26 ... {'hash': 'ddd78924a1e15ad98b28342987d266e95bad... 98 119b098e2e980a229e139a9ed01a469e518e6f26 ... {'hash': '0cabee6178c031c259609ce33864e5db60f1... 99 119b098e2e980a229e139a9ed01a469e518e6f26 ... {'hash': 'ca0eac93cb73d28872aa2ce644a2f1cfbbe2... [100 rows x 8 columns]
You can learn more about Pandas in my 5-minutes to Pandas tutorial. Have fun!
You can actually apply your crypto skills by becoming a cryptofreelancer. If this interests you, check out my free webinar where I show you my journey towards a thriving online coding business. If I can do it, you can too!