kjsmita6
kjsmita6

Reputation: 474

Object returning undefined

So I am trying to read a value from a JSON response I am getting from a website. I am trying to get the clan_name from the response. This code

console.log(JSON.stringify(this.steamFriends.clanStates[groupID]));

where groupID is "103582791438731217", returns this object

{
  "steamid_clan": "103582791438731217",
  "clan_account_flags": 3,
  "name_info": {
    "clan_name": "Chat Bot Testing & Development",
    "sha_avatar": {
      "type": "Buffer",
      "data": [
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0
      ]
    }
  },
  "user_counts": {
    "members": 20,
    "online": 9,
    "chatting": 7,
    "in_game": 5
  },
  "events": [],
  "announcements": []
}

I am using the code JSON.stringify(this.steamFriends.clanStates[groupID].name_info.clan_name to get the clan_name value from it, but I always get the error "Cannot read name_info of undefined."

Any help on this? Thanks.

Upvotes: 1

Views: 53

Answers (1)

Josh Beam
Josh Beam

Reputation: 19772

JSON.stringify returns a String object.

If the response returns a JSON object, then it's already ready to use... simply use normal object accessor methods to query the property you need. No need to use JSON.stringify.

Upvotes: 4

Related Questions