Hello. I will need assistance with figuring out how to use parameters to read a different part of a Json file output. My project involves getting a response from coinmarketcap api and getting coin price value. See below sample. This part works fine.
C#
while(sqlReader.Read())
{
dynamic jObj = JsonConvert.DeserializeObject(response);
decimal jUSDPrice = jObj.data.BTC[0].quote.USD.price;
}Now, my challenge is to use a parameter CoinCode instead of hardcoded BTC so every time sql reader reads new record it will pull our new coin price eg ETH,XRP etc.
How do I use a variable so it looks similar to below?
C#
decimal jUSDPrice = jObj.data.CoinName[0].quote.USD.price;Your help will be much appreciated.
CC


Daniel WrightRank #2,947 · 489 reputationPosted Feb 26, 2025, 5:48 PM EST
Certainly! You are on the right track by wanting to use a parameter to dynamically read different parts of a Json output based on the CoinCode provided. To achieve this in C#, you can use the following approach:
while (sqlReader.Read()) { dynamic jObj = JsonConvert.DeserializeObject(response); // Assuming CoinCode is a variable holding the coin's code like "ETH", "XRP", etc. string coinCode = sqlReader["CoinCode"].ToString(); // Assuming "CoinCode" is the column name from SQL Reader decimal jUSDPrice = jObj.data[coinCode][0].quote.USD.price; }In this code snippet:
1. We first retrieve the coin code from the SQL Reader assuming it is stored in a column named "CoinCode."
2. Then, we use this coin code dynamically to access the corresponding part of the Json output using `jObj.data[coinCode]`.
This way, every time the SQL Reader reads a new record, the `CoinCode` parameter will determine which coin's price value to extract from the Json output.
I hope this helps clarify how you can use parameters to read different parts of a Json output based on the CoinCode provided. Let me know if you need further assistance or more explanation!
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 28, 2025, 7:28 AM EST
To dynamically access a part of a JSON object using a variable (e.g.,
CoinCode), you can use indexer syntax in C#. This allows you to replace the hardcoded key (like"BTC") with a variable. Here's how you can modify your code to achieve this:Use Indexer Syntax :
BTC), you can use a variable (e.g.,CoinCode) to dynamically access the corresponding part of the JSON object.[]) to access properties dynamically.Modify Your Code :
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 28, 2025, 7:29 AM EST
while (sqlReader.Read()) { // Assuming CoinCode is read from the SQL reader string coinCode = sqlReader["CoinCode"].ToString(); // Example: "BTC", "ETH", "XRP" // Deserialize the JSON response into a dynamic object dynamic jObj = JsonConvert.DeserializeObject(response); // Use the variable `coinCode` to dynamically access the JSON property decimal jUSDPrice = jObj.data[coinCode][0].quote.USD.price; // Output or process the price as needed Console.WriteLine($"The price of {coinCode} is {jUSDPrice}"); }Dynamic Access with Indexer Syntax :
dataobject in your JSON is essentially a dictionary-like structure where keys are coin codes (e.g.,"BTC","ETH").jObj.data[coinCode]allows you to dynamically access the value associated with the key stored in thecoinCodevariable.SQL Reader Integration :
sqlReader["CoinCode"]retrieves the coin code (e.g.,"BTC","ETH") from the current record in the SQL result set.Deserialization :
JsonConvert.DeserializeObject(response) converts the JSON string into a dynamic object, enabling flexible access to its properties.Tuhin PaulRank #7 · 71.9K reputationPosted Feb 28, 2025, 7:29 AM EST
Assume the JSON response from the CoinMarketCap API looks like this:
{ "data": { "BTC": [ { "quote": { "USD": { "price": 27000.50 } } } ], "ETH": [ { "quote": { "USD": { "price": 1800.75 } } } ] } }coinCode = "BTC", the code accessesjObj.data["BTC"][0].quote.USD.price.coinCode = "ETH", the code accessesjObj.data["ETH"][0].quote.USD.price.Tuhin PaulRank #7 · 71.9K reputationPosted Feb 28, 2025, 7:30 AM EST
By using indexer syntax (
jObj.data[coinCode]), you can dynamically access JSON properties based on a variable. This approach integrates seamlessly with your SQL reader and allows you to process multiple coin codes efficiently.