I think I'm going crazy... I'm just trying to do some basic API learning for NodeJS and I've got this, which works fine and prints "United States Dollar" to console...
app.get("/", function(req, res){
const url = "https://api.coindesk.com/v1/bpi/currentprice.json"
https.get(url, function(response){
console.log(response.statusCode);
response.on("data", function(data){
const priceData = JSON.parse(data);
console.log(priceData.bpi.USD.description);
})
})
but when I'm trying to access it by using the array position of USD (which is [0])like this...
app.get("/", function(req, res){
const url = "https://api.coindesk.com/v1/bpi/currentprice.json"
https.get(url, function(response){
console.log(response.statusCode);
response.on("data", function(data){
const priceData = JSON.parse(data);
console.log(priceData.bpi[0].description);
})
})
I get a crash of ...
TypeError: Cannot read property 'description' of undefined
The JSON is
{
"time": {
"updated": "Oct 21, 2021 16:10:00 UTC",
"updatedISO": "2021-10-21T16:10:00 00:00",
"updateduk": "Oct 21, 2021 at 17:10 BST"
},
"disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
"chartName": "Bitcoin",
"bpi": {
"USD": {
"code": "USD",
"symbol": "$",
"rate": "63,222.0050",
"description": "United States Dollar",
"rate_float": 63222.005
},
"GBP": {
"code": "GBP",
"symbol": "£",
"rate": "45,779.4964",
"description": "British Pound Sterling",
"rate_float": 45779.4964
},
"EUR": {
"code": "EUR",
"symbol": "€",
"rate": "54,306.7540",
"description": "Euro",
"rate_float": 54306.754
}
}
}
The USD object is position[0] of the bpi array (right?) so why can't I just tap into it like above? This example seems pretty similar to mine so can't see where I'm going wrong?
CodePudding user response:
bpi is not an Array but an object. If it uses curly brackets it is an object. If using square brackets it is an array. To access the USD property of bpi object you use dot notation (like you have done: bpi.USD
) or you use bpi["USD"]
CodePudding user response:
The problem is the content of priceData.bpi
is not an array []
, it's an object {}
, so you can't access USD
by position, the only way to access it is priceData.bpi.USD
.
More about JS arrays, objects.
However, if you really need to access it by position, you can convert the contents of priceData.bpi
into an array using the Object.entries()
method.
Example
let priceData = {"time":{"updated":"Oct 21, 2021 16:39:00 UTC","updatedISO":"2021-10-21T16:39:00 00:00","updateduk":"Oct 21, 2021 at 17:39 BST"},"disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org","chartName":"Bitcoin","bpi":{"USD":{"code":"USD","symbol":"$","rate":"63,340.4233","description":"United States Dollar","rate_float":63340.4233},"GBP":{"code":"GBP","symbol":"£","rate":"45,865.2439","description":"British Pound Sterling","rate_float":45865.2439},"EUR":{"code":"EUR","symbol":"€","rate":"54,408.4735","description":"Euro","rate_float":54408.4735}}}
let bpiArr = Object.entries(priceData.bpi)
console.log(bpiArr[0][1].description)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
Note: the first [0]
element of bpiArr[0]
contains the string "USD" and the second [1]
contains an object which got the description key and its value.
CodePudding user response:
Based on your JSON bpi is an object but you are accessing it via array index which is wrong. If you want the value of USD, the way you were accessing in the first snippet is correct. However, if you want to use it as an array, you can do like below:
const {bpi= {}} = json;
const bpiArr = Object.entries(bpi);
// to access all the values
for (const [key,val] of bpiArr) {
console.log(key,val.description)
}