API: extract a certain value out of the data

A student of mine wants to get the current exchange rate for her app.

Example:
https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=BTC&to_currency=CNY&apikey=demo

How do you extract the Exchange Rate (approximately 50 000)?

Thanks in advance for your help!

Here is one way:

Original Posted Image

Corrected Image:

1 Like

Thank you very much for this immediate answer, Tim!

I have built a similar app according to your suggestion and it works.
I see that you have added some comments. Would you mind sharing the aia-file?

Thanks again!

Just a detail:
get responseContent instead of the textbox on the right, correct (for real time data)?

Yes, my error :slight_smile:

I would give more points to a student who learned more about JSON for this example:
Capture global Exchange_Rate global JSONresponse global Realtime_Currency_Exchange_Rate global URL JSON_exchange_rate.aia (2.8 KB) when Button1 Click

The demo web URL stopped responding after a couple of times, so I force fed the JSONDecode from previously captured JSON text:
{
“Realtime Currency Exchange Rate”: {
“1. From_Currency Code”: “BTC”,
“2. From_Currency Name”: “Bitcoin”,
“3. To_Currency Code”: “CNY”,
“4. To_Currency Name”: “Chinese Yuan”,
“5. Exchange Rate”: “50089.58820000”,
“6. Last Refreshed”: “2020-04-17 02:22:06”,
“7. Time Zone”: “UTC”,
“8. Bid Price”: “50087.11167000”,
“9. Ask Price”: “50092.63079400”
}
}

Note the two JSON tags in global variables used to navigate the decoded JSON response,
with friendlier names compared to error-prone counting.
JSON does not guarantee the order of tag/value pairs, hence the lookup by name.

2 Likes

I like your approach with the use of a dictionary!

Thank you very much for this alternative solution!

(I forgot to go back and clean up some sloppiness on my part …)
My variable names for the two JSON tag constants should have included “TAG” to distinguish them from whatever variable I would use to hold the associated values.
It was past bed time, and I was in a rush.

No problem, Abraham!
Everyone codes slightly differently and has different “standards”.

The important thing is the idea:

  • JSON to dictionary
  • get exchange rate entry

Thanks again for the idea with the dictionary - I like it!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.