Access a key in json

Asked

Viewed 54 times

-4

So. I’m working with the Youtube api to give GET the number of subscribers on a channel. All right. json gets to the code, my only problem is trying to get the value of the key subscriberCount.

The json that is returned:

{
  "kind": "youtube#channelListResponse",
  "etag": "ji3OqKq0t01wxWAUgA2LLg5GR8Q",
  "pageInfo": {
    "resultsPerPage": 1
  },
  "items": [
    {
      "kind": "youtube#channel",
      "etag": "37_CPNyQ9CbrVL8e1YBzPufbOUE",
      "id": "UCasElAZ8Fn1ACje99uyQK5g",
      "statistics": {
        "viewCount": "63842823",
        "commentCount": "0",
        "subscriberCount": "228000",
        "hiddenSubscriberCount": false,
        "videoCount": "349"
      }
    }
  ]
}

This is my code:

let url = "https://www.googleapis.com/youtube/v3/channels?part=statistics&id=" + id_canal + "&key=" + config.youtubetoken
request(url, function (error, response, body) {
   console.log(body.items[0].statistics.subscriberCount);
});

and my mistake is this:

                console.log(body.items[0].statistics.subscriberCount);
                                  ^
TypeError: Cannot read property '0' of undefined

I tried support in the American stack overflow, but they gave me a simple "duplicate topic" when the topic had nothing to do with mine. Please help me out.

Thank you, Gustavo.

  • 1

    Ever tried to give a console.log(body) to see what you’re actually trying to access? The error itself tells exactly what’s going on - TypeError: Cannot read property '0' of undefined shows that body.items is undefined. Because of that, you can’t do body.items[0]. It’s the same thing undefined[0]. Hence the importance of giving the console.log or some other form of debug. The structure of body probably not what you expect.

  • it seems that you receive an object and not an array, take the [0] and see if body.items.statistics.subscriberCount gives you something back.

  • @Colasanto He gives me an error. http://prntscr.com/us5umj

  • @Luizfelipe this is the body returned by the api and sent to the console: http://prntscr.com/us5uzk

  • and body as @Luizfelipe said ? returns something ?

  • 1

    typeof body is object or string?

  • @Luizfelipe string (http://prntscr.com/us5xqs -- http://prntscr.com/us5xtu)

  • 1

    There’s the problem. Convert the string for JSON or hit request to provide the data in the appropriate type.

  • I was at 3 o'clock in the afternoon wondering what the problem might be, and you solved it in 11 minutes. Really, it helped a lot :) @Luizfelipe

  • items is an array, you need to iterate (for example) to read the values, or use the index: body.items[0].statistics

Show 5 more comments

1 answer

2


If you are using lib request, the problem is that the function’s "body" parameter is a string, not a Javascript object. You need to turn it into an object before accessing the props of it. Try:

request(url, function (error, response, body) {
   //Faz parsing do valor de body, para obter um objeto
   const data = JSON.parse(body);

   //Acessa o array items que está dentro de 'data'
   console.log(data.items[0].statistics.subscriberCount);
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.