Coinmarketcap Data Nested Dictionary
Hi I am still in the beginner stages of learning python. I am trying to extract data from Coinmarketcap.com using their api system. I am able to get an output with one large dictio
Solution 1:
In Python, you can access the value of the dictionary by simply doing
value = dict[key]
In your case, you have a nested JSON. You can access the values by chaining the keys.
Your JSON looks like
{
"status": {
"timestamp": "2019-07-17T20:54:40.829Z",
"error_code": 0,
"error_message": null,
"elapsed": 7,
"credit_count": 1
},
"data": {
"ADA": {
"id": 2010,
"name": "Cardano",
"symbol": "ADA",
"slug": "cardano",
"num_market_pairs": 90,
"date_added": "2017-10-01T00:00:00.000Z",
"tags": ["mineable"],
"max_supply": 45000000000,
"circulating_supply": 25927070538,
"total_supply": 31112483745,
"platform": null,
"cmc_rank": 12,
"last_updated": "2019-07-17T20:54:04.000Z",
"quote": {
"USD": {
"price": 0.056165857414,
"volume_24h": 102375843.427606,
"percent_change_1h": -0.816068,
"percent_change_24h": 5.42849,
"percent_change_7d": -21.8139,
"market_cap": 1456216147.0000284,
"last_updated": "2019-07-17T20:54:04.000Z"
}
}
}
}
}
You can access the price as
price = data['data']['ADA']['quote']['USD']['price']
Hope it helps
Solution 2:
Despite similarity it isn't a dictionary as the built-in of Python. Is a JSON. You can parse by the 'key' values. Example:
import json
a = '{"status": {"timestamp": "2019-07-17T20:54:40.829Z", "error_code": 0, "error_message": null, "elapsed": 7}}'
b = json.loads(a)
print(b["status"]["elapsed"])
See that once you're already using requests you don't have to import json module. Ex:
requests.get(url).json()[0]["your_target"])
Analyse the response you get, maybe the index '0' does not apply.
Post a Comment for "Coinmarketcap Data Nested Dictionary"