0

The Json file i want to extract "rank" information from

Basically I would like to get the top 10 ranks cryptocurrencies names. each cryptocurrency has its rank in the given json screenshot. is there anyway I can implement this in python?

Providing a link to the image shown https://api.nomics.com/v1/currencies/ticker?key=demo-26240835858194712a4f8cc0dc635c7a

aaryamann
  • 77
  • 6

3 Answers3

0

Try this:

import json
# your saved json response from API
file_path = "full/path/to/file.json"
with open(file_path, "r") as f:
    data = json.load(f)
    top_10_names = [x["name"] for x in data[:10]]
    # since the data is ordered by rank, 
    # you can take only the first 10 elements.
    print(top_10_names)

Gabio
  • 9,126
  • 3
  • 12
  • 32
0
import requests
import json

resp = requests.get(url = "https://api.nomics.com/v1/currencies/ticker", params = {"key" :"demo-26240835858194712a4f8cc0dc635c7a"}) # Fetch response from API
resp = json.loads(resp.text)
final_names = []
for i in range(10): # JSON is ordered via Rank
    final_names.append(resp[i]["name"])

print(final_names) # Print final names

Hope this answers your question!!!!

skaul05
  • 2,154
  • 3
  • 16
  • 26
0

Solution if cryptocurrencies are not sorted by rank by default in your json:

import requests
from pprint import pprint

top_all = []
data = requests.get(url="https://api.nomics.com/v1/currencies/ticker",
   params={"key": "demo-26240835858194712a4f8cc0dc635c7a"}).json()

for row in data:
    top_all.append({"name": row["name"], "rank": row["rank"]})

top_all = sorted(top_all, key=lambda x: int(x["rank"]))
pprint(top_all[0:10])
Maciej A. Czyzewski
  • 1,539
  • 1
  • 13
  • 24