Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: list indices must be integers or slices, not str API, Json in Python

I am trying to get data from API but facing an error. I need block height from this API.

import requests
import json


fetch_json_net = requests.get('https://api.minaexplorer.com/blocks?limit=1')
blk_height_net = fetch_json_net.json()["blocks"]["blockHeight"]
print(blk_height_net)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

First of all, I checked what fetch_json_net.json() returns:

{'blocks': [{'blockHeight': 115919, 'canonical': True, ...

It means blk_height_net["blocks"] has a list value, not a dictionary value.

Therefore, you can get the blockHeight value, as follows:

import requests
import json

fetch_json_net = requests.get('https://api.minaexplorer.com/blocks?limit=1')
blk_height_net = fetch_json_net.json()["blocks"][0]["blockHeight"]
print(blk_height_net)
# 115919
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading