Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialjun cheng wong
17,021 PointsHow can I access JSON key with spaces
Below is the json data I am going to parse.
{
"Global Quote": {
"01. symbol": "AAPL",
"02. open": "125.5700",
"03. high": "125.8000",
"04. low": "124.5500",
"05. price": "124.6100",
"06. volume": "71311109",
"07. latest trading day": "2021-05-28",
"08. previous close": "125.2800",
"09. change": "-0.6700",
"10. change percent": "-0.5348%"
}
}
Normally, we can access the data using dot notation. However, given that there are spaces inside the key, we have to use squared bracket instead of dot notation. When I use
console.log(stock)
after parsing the data and assign it to a variable called "stock", it returns all the data the same as above.
However, when I tried to access data using:
console.log(stock['01. symbol'])
it returns undefined.
Any idea or suggestion?
1 Answer
Cory Harkins
16,500 PointsHi! You are definitely thinking correctly, however, try stock["Global Quote"]["01. symbol"]
as "01. symbol" is a nested property of the "Global Quote" object. :)
jun cheng wong
17,021 Pointsjun cheng wong
17,021 PointsYeap, I have figured it out. It was completely ignored by me