Skip to content
Advertisement

I cannot find a way to print a JSON Value

{ "data": { "time": { "updated": "May 20, 2022 07:29:00 UTC", "updatedISO": "2022-05-20T07:29:00+00:00", "updateduk": "May 20, 2022 at 08:29 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": "30,177.6870", "description": "United States Dollar", "rate_float": 30177.687 }, "GBP": { "code": "GBP", "symbol": "£", "rate": "24,661.9301", "description": "British Pound Sterling", "rate_float": 24661.9301 }, "EUR": { "code": "EUR", "symbol": "€", "rate": "29,026.2574", "description": "Euro", "rate_float": 29026.2574 } } }, "status": 200, "statusText": "", "headers": { "cache-control": "max-age=15", "content-length": "678", "content-type": "application/javascript", "expires": "Fri, 20 May 2022 07:30:07 UTC" }, "config": { "transformRequest": {}, "transformResponse": {}, "timeout": 0, "xsrfCookieName": "XSRF-TOKEN", "xsrfHeaderName": "X-XSRF-TOKEN", "maxContentLength": -1, "headers": { "Accept": "application/json, text/plain, */*" }, "method": "get", "url": "https://api.coindesk.com/v1/bpi/currentprice.json" }, "request": {} }

I have this JSON object that prints the information about currencies. I am trying to get the data for a specfic currency that is USD.

When I type JSON.data.bpi.USD it says UNDEFINED. I need help with this.

Advertisement

Answer

You can access to your object properties with Property accessor dot notation:

const obj = { "data": { "time": { "updated": "May 20, 2022 07:29:00 UTC", "updatedISO": "2022-05-20T07:29:00+00:00", "updateduk": "May 20, 2022 at 08:29 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": "30,177.6870", "description": "United States Dollar", "rate_float": 30177.687 }, "GBP": { "code": "GBP", "symbol": "£", "rate": "24,661.9301", "description": "British Pound Sterling", "rate_float": 24661.9301 }, "EUR": { "code": "EUR", "symbol": "€", "rate": "29,026.2574", "description": "Euro", "rate_float": 29026.2574 } } }, "status": 200, "statusText": "", "headers": { "cache-control": "max-age=15", "content-length": "678", "content-type": "application/javascript", "expires": "Fri, 20 May 2022 07:30:07 UTC" }, "config": { "transformRequest": {}, "transformResponse": {}, "timeout": 0, "xsrfCookieName": "XSRF-TOKEN", "xsrfHeaderName": "X-XSRF-TOKEN", "maxContentLength": -1, "headers": { "Accept": "application/json, text/plain, */*" }, "method": "get", "url": "https://api.coindesk.com/v1/bpi/currentprice.json" }, "request": {} }

console.log(obj.data.bpi.USD)
Advertisement