pythonjsondictionary

Coinmarketcap data nested dictionary


I am trying to extract data from coinmarketcap.com using their API. I am able to get an output with one large dictionary but can't seem to figure out how to pull specific data. I would like to receive just the price and last_updated entries.

I have tried referencing .loads and slicing the data into a list. I have also tried to index into the dictionaries, but the nested dictionaries in the output are making it difficult for me to understand. I have watched many YouTube tutorials and googled for help, but am unable to find a solution.

import requests
import json

url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest'
api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'

headers = {'Accepts': 'application/json', 'X-CMC_PRO_API_KEY': api_key}
parameters = {'symbol': 'ADA'}

response = requests.get(url, headers = headers, params = parameters)

data = response.json()
data_str = json.dumps(data, indent = 2)

print(data_str)

Here is the output of the dictionary:

{
  "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"
        }
      }
    }
  }
}

Solution

  • You can access the value of the dictionary simply by

    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']