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.
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?
decimal jUSDPrice = jObj.data.CoinName[0].quote.USD.price;
Your help will be much appreciated.
CC

Daniel WrightPosted Feb 26, 2025, 10:48 PM
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:
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 PaulPosted Feb 28, 2025, 12:30 PM
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.Tuhin PaulPosted Feb 28, 2025, 12:29 PM
Assume the JSON response from the CoinMarketCap API looks like this:
coinCode = "BTC", the code accessesjObj.data["BTC"][0].quote.USD.price.coinCode = "ETH", the code accessesjObj.data["ETH"][0].quote.USD.price.Tuhin PaulPosted Feb 28, 2025, 12:29 PM
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 PaulPosted Feb 28, 2025, 12:28 PM
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 :