How do I use JSON.parse()
properly in JavaScript? The current code I have should access the games
object
and then give an output of the games object name
. Why is my code returning [object Object]
?
let games = '{"games": [{"name": "Funny Game","desc": "A game to make you laugh!"}]}'
JSON.parse(games)
console.log(games[0].name);
//Should output Name
I’ve looked at Accessing json objects with no name and used to try and solve also.
2
Answers
The issue here is because you call
JSON.parse()
, but don’t do anything with its output. You need to assign it to something, then interrogate that object to find thename
property. Here’s a working example:Note that I changed the variable names to avoid confusion between the
games
variable holding the JSON string and thegames
property holding the array of objects.Here is how you access it, there is an inner key "games"