Getting currency rate to #text

I’d like to get API currency rate to one of the texts on my site. I use:

let NBPurl = “http://api.nbp.pl/api/exchangerates/rates/A/USD/?format=json”;
//it returns: {“table”:“A”,“currency”:“dolar amerykański”,“code”:“USD”,
// “rates”:[{“no”:“232/A/NBP/2018”,“effectiveDate”:“2018-11-29”,“mid”:3.7728}]}
fetch(NBPurl, {method: ‘get’})
.then(response => response.json())
.then(json => $w(“#text75”).text = json.rates[‘mid’].toString()); //here problem I think

but it does not work…
Any help?
This must be simple.

Hi,
The json.rates object is an array with on cell. Therefore, you should use json.rates[0].mid instead:

let NBPurl = "http://api.nbp.pl/api/exchangerates/rates/A/USD/?format=json";

    fetch(NBPurl, { method: 'get' })
        .then(response => response.json())
        .then(json => {
            $w("#text17").text = json.rates[0].mid.toString()
        });

I hope it’s clear.

Best,
Tal.

Fantastic! Thank You Tal.

I developed my code to show exchanged price from dataset multiplied by currency rate from bank:

$w.onReady( function () {
getCurrency() // is in backend
.then( (json) => {
var rate = String(parseFloat(json.rates[0].ask.toString()))
var cena1 = String(parseFloat($w(“#dynamicDataset”).getCurrentItem().price1))
var cenaPLN = String(rate*cena1)
var cenazero = cenaPLN.toFixed(0) // problem is here
$w(“#text77”).text = cenazero + ’ PLN’
})
$w(“#dataset1”).onAfterSave(sendFormData);
});

…but I have problem to round final price.

Any help?

OK, I got it:

var cenazero = Math.round(cenaPLN)