Struggling with my currency conversion project, I'm trying to display JSON response results on my website but can't seem to make it work. The code snippet below,
.then((response) => {
return response.json();
})
.then((jsonResponse) => {
let objData = {
amount: '',
from: '',
to: '',
result: '',
};
window.console.log('jsonresponse ==>' + JSON.stringify(jsonResponse));
let exchangeData = jsonResponse['query'];
window.console.log('exchangeData==> ' + JSON.stringify(exchangeData))
objData.amount = exchangeData['amount'];
objData.from = exchangeData['from'];
objData.to = exchangeData['to'];
objData.result = exchangeData['result'];
this.conversionData = objData;
window.console.log('objData=' + JSON.stringify(objData));
}).catch(error => {
window.console.log('callout error ' + JSON.stringify(error));
})
}
}
currently only fetches 'amount', 'from', and 'to' without getting the 'result'. How do I modify it to also include the conversion result? Here is a sample of the JSON data:
{ "info": { "quote": 0.975625, "timestamp": 1669042263 },
"query": { "amount": 5, "from": "USD", "to": "EUR" },
"result": 4.878125,
"success": true
}
The current output retrieves only:
"query": {
"amount": 5,
"from": "USD",
"to": "EUR"
Please guide me on how to retrieve the complete data including the result.
I attempted the following approach that did not yield the desired outcome:</p>
const obj1={"query":[ { "amount":"", "from": "", "to": "" }, ] };
const obj2={"result":{}, };
const response = JSON.parse(JSON.stringify(obj1));
response.query.push(...obj2.result); console.log(response);