How to Get JSON from URL in Python?

5/5 - (1 vote)

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.

  1. Import the modules urllib.request and json.
  2. Open the connection to the server in a with environment by running with urllib.request.urlopen(your_url) as url:
  3. Load the data from the server via json.loads( 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 ''.

import urllib.request
import json 

# Bitcoin Genesis Block Transactions
your_url = ''

with urllib.request.urlopen(your_url) as url:
    data = json.loads(

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:

How to Get JSON from URL in Python?
Click to Run

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 = '' + your_btc_address


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!