JSON Dictionary accessing key values

I am very new to Xojo and JSON and trying to extract some values at specific keys.
The JSON string is returned from an API using ContentReceived with URLConnection.

I am trying to return the following key values for each crypto coin:

  "name": "Bitcoin",
  "symbol": "BTC",

and

  "quote": {
             "USD": {
             "price": 21125.33523917748,

Can anyone please point me in the right direction?
Thanks


// This seems to return the two keys "status" and "data" but 
// I am struggling to get below the "data" key to the underlying data.

Var jsonContent as Dictionary
// "content" is the string returned by ContentReceived of URLConnection
jsonContent = ParseJSON(content)

For Each key As Variant In jsonContent.Keys
    System.DebugLog("key: " + key)   // returns "status" and "data" keys
Next


// The JSON
{
   "status": {
      "timestamp": "2022-08-22T09:47:30.639Z",
      "error_code": 0,
      "error_message": null,
      "elapsed": 20,
      "credit_count": 1,
      "notice": null,
      "total_count": 9651
   },
   "data": [
      {
         "id": 1,
         "name": "Bitcoin",
         "symbol": "BTC",
         "slug": "bitcoin",
         "num_market_pairs": 9682,
         "date_added": "2013-04-28T00:00:00.000Z",
         "tags": [
            "mineable",
            "pow",
            "sha-256",
            "store-of-value",
            "state-channel",
            "coinbase-ventures-portfolio",
            "three-arrows-capital-portfolio",
            "polychain-capital-portfolio",
            "binance-labs-portfolio",
            "blockchain-capital-portfolio",
            "boostvc-portfolio",
            "cms-holdings-portfolio",
            "dcg-portfolio",
            "dragonfly-capital-portfolio",
            "electric-capital-portfolio",
            "fabric-ventures-portfolio",
            "framework-ventures-portfolio",
            "galaxy-digital-portfolio",
            "huobi-capital-portfolio",
            "alameda-research-portfolio",
            "a16z-portfolio",
            "1confirmation-portfolio",
            "winklevoss-capital-portfolio",
            "usv-portfolio",
            "placeholder-ventures-portfolio",
            "pantera-capital-portfolio",
            "multicoin-capital-portfolio",
            "paradigm-portfolio"
         ],
         "max_supply": 21000000,
         "circulating_supply": 19128487,
         "total_supply": 19128487,
         "platform": null,
         "cmc_rank": 1,
         "self_reported_circulating_supply": null,
         "self_reported_market_cap": null,
         "tvl_ratio": null,
         "last_updated": "2022-08-22T09:45:00.000Z",
         "quote": {
            "USD": {
               "price": 21125.33523917748,
               "volume_24h": 2.5527514404854446E10,
               "volume_change_24h": 1.3919,
               "percent_change_1h": 0.31512491,
               "percent_change_24h": -1.41741563,
               "percent_change_7d": -12.19602879,
               "percent_change_30d": -6.90168759,
               "percent_change_60d": 3.00817049,
               "percent_change_90d": -27.83078588,
               "market_cap": 4.040957004932483E11,
               "market_cap_dominance": 40.3036,
               "fully_diluted_market_cap": 4.4363204002273E11,
               "tvl": null,
               "last_updated": "2022-08-22T09:45:00.000Z"
            }
         }
      },
      {
         "id": 1027,
         "name": "Ethereum",
         "symbol": "ETH",
         "slug": "ethereum",
         "num_market_pairs": 6021,
         "date_added": "2015-08-07T00:00:00.000Z",
         "tags": [
            "mineable",
            "pow",
            "smart-contracts",
            "ethereum-ecosystem",
            "coinbase-ventures-portfolio",
            "three-arrows-capital-portfolio",
            "polychain-capital-portfolio",
            "binance-labs-portfolio",
            "blockchain-capital-portfolio",
            "boostvc-portfolio",
            "cms-holdings-portfolio",
            "dcg-portfolio",
            "dragonfly-capital-portfolio",
            "electric-capital-portfolio",
            "fabric-ventures-portfolio",
            "framework-ventures-portfolio",
            "hashkey-capital-portfolio",
            "kenetic-capital-portfolio",
            "huobi-capital-portfolio",
            "alameda-research-portfolio",
            "a16z-portfolio",
            "1confirmation-portfolio",
            "winklevoss-capital-portfolio",
            "usv-portfolio",
            "placeholder-ventures-portfolio",
            "pantera-capital-portfolio",
            "multicoin-capital-portfolio",
            "paradigm-portfolio",
            "injective-ecosystem",
            "bnb-chain"
         ],
         "max_supply": null,
         "circulating_supply": 1.220636293115E8,
         "total_supply": 1.220636293115E8,
         "platform": null,
         "cmc_rank": 2,
         "self_reported_circulating_supply": null,
         "self_reported_market_cap": null,
         "tvl_ratio": null,
         "last_updated": "2022-08-22T09:45:00.000Z",
         "quote": {
            "USD": {
               "price": 1557.820987233605,
               "volume_24h": 1.6842357840483316E10,
               "volume_change_24h": -5.3868,
               "percent_change_1h": 0.6868271,
               "percent_change_24h": -3.38632385,
               "percent_change_7d": -17.80324636,
               "percent_change_30d": -0.36344873,
               "percent_change_60d": 42.08418926,
               "percent_change_90d": -20.84404233,
               "market_cap": 1.9015328351935773E11,
               "market_cap_dominance": 18.9649,
               "fully_diluted_market_cap": 1.9015328351936E11,
               "tvl": null,
               "last_updated": "2022-08-22T09:45:00.000Z"
            }
         }
      },
      {
         "id": 825,
         "name": "Tether",
         "symbol": "USDT",
         "slug": "tether",
         "num_market_pairs": 38586,
         "date_added": "2015-02-25T00:00:00.000Z",
         "tags": [
            "payments",
            "stablecoin",
            "asset-backed-stablecoin",
            "avalanche-ecosystem",
            "solana-ecosystem",
            "arbitrum-ecosytem",
            "moonriver-ecosystem",
            "injective-ecosystem",
            "bnb-chain",
            "usd-stablecoin"
         ],
         "max_supply": null,
         "circulating_supply": 6.754955501574651E10,
         "total_supply": 6.915777703300569E10,
         "platform": {
            "id": 1027,
            "name": "Ethereum",
            "symbol": "ETH",
            "slug": "ethereum",
            "token_address": "0xdac17f958d2ee523a2206206994597c13d831ec7"
         },
         "cmc_rank": 3,
         "self_reported_circulating_supply": null,
         "self_reported_market_cap": null,
         "tvl_ratio": null,
         "last_updated": "2022-08-22T09:45:00.000Z",
         "quote": {
            "USD": {
               "price": 1.0000502690244426,
               "volume_24h": 4.569906400156022E10,
               "volume_change_24h": -28.9307,
               "percent_change_1h": 0.00478752,
               "percent_change_24h": -0.00290157,
               "percent_change_7d": -0.02247193,
               "percent_change_30d": -2.5213E-4,
               "percent_change_60d": 0.08299106,
               "percent_change_90d": 0.10214316,
               "market_cap": 6.755295066597868E10,
               "market_cap_dominance": 6.738,
               "fully_diluted_market_cap": 6.916125352699E10,
               "tvl": null,
               "last_updated": "2022-08-22T09:45:00.000Z"
            }
         }
      },
      {
         "id": 1274,
         "name": "Waves",
         "symbol": "WAVES",
         "slug": "waves",
         "num_market_pairs": 197,
         "date_added": "2016-06-02T00:00:00.000Z",
         "tags": [
            "lpos",
            "platform",
            "smart-contracts"
         ],
         "max_supply": null,
         "circulating_supply": 109125346,
         "total_supply": 109125346,
         "platform": null,
         "cmc_rank": 72,
         "self_reported_circulating_supply": null,
         "self_reported_market_cap": null,
         "tvl_ratio": null,
         "last_updated": "2022-08-22T09:45:00.000Z",
         "quote": {
            "USD": {
               "price": 4.671080074481326,
               "volume_24h": 1.3784033440708837E8,
               "volume_change_24h": 5.2141,
               "percent_change_1h": 0.47570136,
               "percent_change_24h": -4.90440394,
               "percent_change_7d": -21.52658174,
               "percent_change_30d": -15.97785721,
               "percent_change_60d": -25.64810851,
               "percent_change_90d": -16.74833652,
               "market_cap": 5.0973322932148045E8,
               "market_cap_dominance": 0.0508,
               "fully_diluted_market_cap": 5.0973322932E8,
               "tvl": null,
               "last_updated": "2022-08-22T09:45:00.000Z"
            }
         }
      },
      {
         "id": 2634,
         "name": "XDC Network",
         "symbol": "XDC",
         "slug": "xinfin",
         "num_market_pairs": 59,
         "date_added": "2018-04-12T00:00:00.000Z",
         "tags": [
            "xdc-ecosystem"
         ],
         "max_supply": null,
         "circulating_supply": 1.230501269875E10,
         "total_supply": 3.770501269875E10,
         "platform": null,
         "cmc_rank": 93,
         "self_reported_circulating_supply": null,
         "self_reported_market_cap": null,
         "tvl_ratio": null,
         "last_updated": "2022-08-22T09:45:00.000Z",
         "quote": {
            "USD": {
               "price": 0.029980741743621045,
               "volume_24h": 6673192.42830843,
               "volume_change_24h": 27.8967,
               "percent_change_1h": 0.01548613,
               "percent_change_24h": -4.7100814,
               "percent_change_7d": -16.18922633,
               "percent_change_30d": 4.83520484,
               "percent_change_60d": 2.02080296,
               "percent_change_90d": -27.62629924,
               "market_cap": 3.689134078732012E8,
               "market_cap_dominance": 0.0368,
               "fully_diluted_market_cap": 1.13042424816E9,
               "tvl": null,
               "last_updated": "2022-08-22T09:45:00.000Z"
            }
         }
      },
      {
         "id": 2682,
         "name": "Holo",
         "symbol": "HOT",
         "slug": "holo",
         "num_market_pairs": 121,
         "date_added": "2018-04-29T00:00:00.000Z",
         "tags": [
            "platform",
            "distributed-computing",
            "filesharing",
            "storage"
         ],
         "max_supply": null,
         "circulating_supply": 1.7334297412673712E11,
         "total_supply": 1.7761943354114133E11,
         "platform": {
            "id": 1027,
            "name": "Ethereum",
            "symbol": "ETH",
            "slug": "ethereum",
            "token_address": "0x6c6ee5e31d828de241282b9606c8e98ea48526e2"
         },
         "cmc_rank": 94,
         "self_reported_circulating_supply": null,
         "self_reported_market_cap": null,
         "tvl_ratio": null,
         "last_updated": "2022-08-22T09:45:00.000Z",
         "quote": {
            "USD": {
               "price": 0.0021177049118466075,
               "volume_24h": 1.55755062508536E7,
               "volume_change_24h": -12.3341,
               "percent_change_1h": 0.49398254,
               "percent_change_24h": -3.6647268,
               "percent_change_7d": -19.62358816,
               "percent_change_30d": -0.21480141,
               "percent_change_60d": -3.52942973,
               "percent_change_90d": -16.98425537,
               "market_cap": 3.670892677422906E8,
               "market_cap_dominance": 0.0366,
               "fully_diluted_market_cap": 3.7614554685E8,
               "tvl": null,
               "last_updated": "2022-08-22T09:45:00.000Z"
            }
         }
      },
      {
         "id": 7080,
         "name": "Gala",
         "symbol": "GALA",
         "slug": "gala",
         "num_market_pairs": 187,
         "date_added": "2020-09-16T00:00:00.000Z",
         "tags": [
            "collectibles-nfts",
            "gaming",
            "binance-smart-chain",
            "polygon-ecosystem",
            "play-to-earn"
         ],
         "max_supply": null,
         "circulating_supply": 6977205436,
         "total_supply": 35240112493,
         "platform": {
            "id": 1027,
            "name": "Ethereum",
            "symbol": "ETH",
            "slug": "ethereum",
            "token_address": "0x15D4c048F83bd7e37d49eA4C83a07267Ec4203dA"
         },
         "cmc_rank": 95,
         "self_reported_circulating_supply": null,
         "self_reported_market_cap": null,
         "tvl_ratio": null,
         "last_updated": "2022-08-22T09:45:00.000Z",
         "quote": {
            "USD": {
               "price": 0.051393595341026006,
               "volume_24h": 1.7998448520986804E8,
               "volume_change_24h": -26.6932,
               "percent_change_1h": 0.49676899,
               "percent_change_24h": -3.64716789,
               "percent_change_7d": -24.16882469,
               "percent_change_30d": -5.13558055,
               "percent_change_60d": -9.32999386,
               "percent_change_90d": -36.03927827,
               "market_cap": 3.585836727889909E8,
               "market_cap_dominance": 0.0358,
               "fully_diluted_market_cap": 1.81111608124E9,
               "tvl": null,
               "last_updated": "2022-08-22T09:45:00.000Z"
            }
         }
      },
      {
         "id": 5692,
         "name": "Compound",
         "symbol": "COMP",
         "slug": "compound",
         "num_market_pairs": 288,
         "date_added": "2020-06-16T00:00:00.000Z",
         "tags": [
            "defi",
            "dao",
            "yield-farming",
            "polkadot-ecosystem",
            "coinbase-ventures-portfolio",
            "three-arrows-capital-portfolio",
            "polychain-capital-portfolio",
            "lending-borowing",
            "dragonfly-capital-portfolio",
            "alameda-research-portfolio",
            "a16z-portfolio",
            "pantera-capital-portfolio",
            "paradigm-portfolio",
            "bnb-chain"
         ],
         "max_supply": 10000000,
         "circulating_supply": 7216671.60434877,
         "total_supply": 10000000,
         "platform": {
            "id": 1027,
            "name": "Ethereum",
            "symbol": "ETH",
            "slug": "ethereum",
            "token_address": "0xc00e94cb662c3520282e6f5717214004a7f26888"
         },
         "cmc_rank": 96,
         "self_reported_circulating_supply": null,
         "self_reported_market_cap": null,
         "tvl_ratio": 0.12452709,
         "last_updated": "2022-08-22T09:45:00.000Z",
         "quote": {
            "USD": {
               "price": 48.50504039956366,
               "volume_24h": 1.1597837939373973E8,
               "volume_change_24h": 28.1738,
               "percent_change_1h": 0.16306953,
               "percent_change_24h": -4.27863131,
               "percent_change_7d": -20.84913745,
               "percent_change_30d": -13.9017162,
               "percent_change_60d": 14.60460866,
               "percent_change_90d": -29.33387076,
               "market_cap": 3.50044947719321E8,
               "market_cap_dominance": 0.0349,
               "fully_diluted_market_cap": 485050404,
               "tvl": 2.81099442740644E9,
               "last_updated": "2022-08-22T09:45:00.000Z"
            }
         }
      },
      {
         "id": 4269,
         "name": "GateToken",
         "symbol": "GT",
         "slug": "gatetoken",
         "num_market_pairs": 24,
         "date_added": "2019-08-20T00:00:00.000Z",
         "tags": [
            "marketplace",
            "centralized-exchange",
            "discount-token",
            "algorand-ecosystem"
         ],
         "max_supply": null,
         "circulating_supply": 7.848578180958357E7,
         "total_supply": 300000000,
         "platform": {
            "id": 1027,
            "name": "Ethereum",
            "symbol": "ETH",
            "slug": "ethereum",
            "token_address": "0xe66747a101bff2dba3697199dcce5b743b454759"
         },
         "cmc_rank": 97,
         "self_reported_circulating_supply": null,
         "self_reported_market_cap": null,
         "tvl_ratio": null,
         "last_updated": "2022-08-22T09:45:00.000Z",
         "quote": {
            "USD": {
               "price": 4.425068588341843,
               "volume_24h": 2555343.85667689,
               "volume_change_24h": -8.6428,
               "percent_change_1h": 1.1051696,
               "percent_change_24h": -0.20827247,
               "percent_change_7d": -10.52855229,
               "percent_change_30d": 8.29668454,
               "percent_change_60d": 4.69144753,
               "percent_change_90d": -21.96694491,
               "market_cap": 3.473049677170399E8,
               "market_cap_dominance": 0.0346,
               "fully_diluted_market_cap": 1.3275205765E9,
               "tvl": null,
               "last_updated": "2022-08-22T09:45:00.000Z"
            }
         }
      },
      {
         "id": 3783,
         "name": "Ankr",
         "symbol": "ANKR",
         "slug": "ankr",
         "num_market_pairs": 124,
         "date_added": "2019-03-06T00:00:00.000Z",
         "tags": [
            "platform",
            "cosmos-ecosystem",
            "enterprise-solutions",
            "defi",
            "filesharing",
            "staking",
            "substrate",
            "binance-chain",
            "polkadot-ecosystem",
            "pantera-capital-portfolio",
            "polygon-ecosystem",
            "celo-ecosystem",
            "bnb-chain"
         ],
         "max_supply": 10000000000,
         "circulating_supply": 9.662899377791666E9,
         "total_supply": 10000000000,
         "platform": {
            "id": 1027,
            "name": "Ethereum",
            "symbol": "ETH",
            "slug": "ethereum",
            "token_address": "0x8290333cef9e6d528dd5618fb97a76f268f3edd4"
         },
         "cmc_rank": 98,
         "self_reported_circulating_supply": null,
         "self_reported_market_cap": null,
         "tvl_ratio": 3.27881769,
         "last_updated": "2022-08-22T09:45:00.000Z",
         "quote": {
            "USD": {
               "price": 0.03576811231277055,
               "volume_24h": 3.350388063629759E7,
               "volume_change_24h": -21.9525,
               "percent_change_1h": 0.11446787,
               "percent_change_24h": -6.68559342,
               "percent_change_7d": -22.70888721,
               "percent_change_30d": 22.9776823,
               "percent_change_60d": 19.52136709,
               "percent_change_90d": -7.63198607,
               "market_cap": 3.4562367021185297E8,
               "market_cap_dominance": 0.0345,
               "fully_diluted_market_cap": 3.5768112313E8,
               "tvl": 1.0541106668799E8,
               "last_updated": "2022-08-22T09:45:00.000Z"
            }
         }
      },
      {
         "id": 1684,
         "name": "Qtum",
         "symbol": "QTUM",
         "slug": "qtum",
         "num_market_pairs": 191,
         "date_added": "2017-05-24T00:00:00.000Z",
         "tags": [
            "platform",
            "smart-contracts",
            "kenetic-capital-portfolio"
         ],
         "max_supply": 107822406,
         "circulating_supply": 1.042977485E8,
         "total_supply": 103944501,
         "platform": null,
         "cmc_rank": 99,
         "self_reported_circulating_supply": null,
         "self_reported_market_cap": null,
         "tvl_ratio": null,
         "last_updated": "2022-08-22T09:45:00.000Z",
         "quote": {
            "USD": {
               "price": 3.293824100497933,
               "volume_24h": 3.451455740030912E7,
               "volume_change_24h": 3.7628,
               "percent_change_1h": 0.23586929,
               "percent_change_24h": -4.02914475,
               "percent_change_7d": -18.93462898,
               "percent_change_30d": -0.87702519,
               "percent_change_60d": 15.20983543,
               "percent_change_90d": -17.32824718,
               "market_cap": 3.435384376369721E8,
               "market_cap_dominance": 0.0343,
               "fully_diluted_market_cap": 3.5514803946E8,
               "tvl": null,
               "last_updated": "2022-08-22T09:45:00.000Z"
            }
         }
      },
      {
         "id": 9903,
         "name": "Convex Finance",
         "symbol": "CVX",
         "slug": "convex-finance",
         "num_market_pairs": 117,
         "date_added": "2021-05-18T00:00:00.000Z",
         "tags": [
            "defi",
            "yield-aggregator"
         ],
         "max_supply": 100000000,
         "circulating_supply": 6.690888578548364E7,
         "total_supply": 9.39340856303394E7,
         "platform": {
            "id": 1027,
            "name": "Ethereum",
            "symbol": "ETH",
            "slug": "ethereum",
            "token_address": "0x4e3fbd56cd56c3e72c1403e103b45db9da5b9d2b"
         },
         "cmc_rank": 100,
         "self_reported_circulating_supply": 2814665,
         "self_reported_market_cap": 1.4059236428216776E7,
         "tvl_ratio": 0.07959748,
         "last_updated": "2022-08-22T09:45:00.000Z",
         "quote": {
            "USD": {
               "price": 4.994994583091336,
               "volume_24h": 1.856832232218705E7,
               "volume_change_24h": 78.4388,
               "percent_change_1h": 0.78315231,
               "percent_change_24h": -6.61877364,
               "percent_change_7d": -25.38230906,
               "percent_change_30d": -36.39932402,
               "percent_change_60d": 12.06818631,
               "percent_change_90d": -51.48606616,
               "market_cap": 3.342095220591677E8,
               "market_cap_dominance": 0.0333,
               "fully_diluted_market_cap": 4.9949945831E8,
               "tvl": 4.19874500113695E9,
               "last_updated": "2022-08-22T09:45:00.000Z"
            }
         }
      }
   ]
}

Use a JSONItem instead of a Dictionary.
As data is an array you have to loop through the childs.
And better use “Lookup” as otherwise you will get an Exception if a key doesn’t exist.

Try
  
  Var json As New JSONItem(content)
  Var data As JSONItem = json.Lookup("data", Nil)
  
  If data <> Nil Then
    
    For i As Integer = 0 To data.Count - 1
      
      Var currentData As JSONItem = data.ChildAt(i)
      
      Var name As String = currentData.Lookup("name", "")
      Var symbol As String = currentData.Lookup("symbol", "")
      
    Next
    
  End If
  
Catch JSONException
  
End Try
2 Likes

Or use the dictionary:

Var JSONContent As Dictionary = Xojo.ParseJSON(Content)
Var Data() As Variant = JSONContent.Value(“data”)

For Each Dict As Dictionary In Data
  Var Name As String = Dict.Value(“name”)
  Var Symbol As String = Dict.Value(“symbol”)
  Var Quote As Dictionary = Dict.Value(“quote”)
Next

This code doesn’t do error checking, which is vulnerable to JSONException if Content is not properly formed, KeyNotFoundException if a key doesn’t exist, and TypeMismatchException if a value is not the correct type. You’ll need to handle those potentialities.

Thanks so much Thom, this works great to extract the Name and Symbol values.
I see you have put Quote (“quote”) in a dictionary. How do I go about drilling down to the Price (“price”) associated with each Name and Symbol?

Thanks so much Stefan, this works well to extract the Name and Symbol values. However I am still not sure how to extract the price associated with each name and symbol.

Keep going. Quote will have values USD and price.

Thanks to both Thom and Stefan for your help!
This seems to work:

Var JSONContent As Dictionary = Xojo.ParseJSON(Content)
Var Data() As Variant = JSONContent.Value("data")

For Each Dict As Dictionary In Data
  Var Name As String = Dict.Value("name")
  Var Symbol As String = Dict.Value("symbol")
  Var Quote As Dictionary = Dict.Value("quote")
  
  // added:
  var quoteDict as Dictionary = Quote.Value("USD")
  var dblPrice as Double = quoteDict.Value("price")
  var strPrice as string = str(dblPrice)
   
  // now do something with the values...
  System.DebugLog(symbol + "   " + name + " " + strPrice)
  ComboBox1.AddRow(Symbol)
     
Next