skip to Main Content

I want to return the followers_count of a twitter user. When I do a get request to the api the JSON I am returned is:

EDIT: There is multiple parts to the response the part I want to extract data from starts like:

body:'[{"id":842072275,"id_str":"842072275","name":"BreaK","screen_name":"BreaK_71","location":"England","description":"Full Time Streamer from the UK","url":"http:\/\/t.co\/YZTfi5U7AE","entities":{"url":{"urls":[{"url":"http:\/\/t.co\/YZTfi5U7AE","expanded_url":"http:\/\/www.twitch.tv\/break71","display_url":"twitch.tv\/break71","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":4159,"friends_count":211,"listed_count":43,"created_at":"Sun Sep 23 17:13:19 +0000 2012","favourites_count":4262,"utc_offset":7200,"time_zone":"Amsterdam","geo_enabled":false,"verified":false,"statuses_count":9763,"lang":"en","status":{"created_at":"Fri Apr 08 17:46:52 +0000 2016","id":718495339188195328,"id_str":"718495339188195328","text":"Hey I'm live with some @BattleRoyaleMod talking hype about the Tournament tomorrow and messing around. Lets gooo! https:\/\/t.co\/4V9CHaTcIK","entities":{"hashtags":[],"symbols":[],"user_mentions":[{"screen_name":"BattleRoyaleMod","name":"PLAYERUNKNOWN","id":1880574811,"id_str":"1880574811","indices":[23,39]}],"urls":[{"url":"https:\/\/t.co\/4V9CHaTcIK","expanded_url":"http:\/\/twitch.tv\/break71","display_url":"twitch.tv\/break71","indices":[114,137]}]},"truncated":false,"source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e","in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":3,"favorite_count":5,"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"},"contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"1A1B1F","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/642608935812374528\/7t7IqgIJ.png","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/642608935812374528\/7t7IqgIJ.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/693388490642423808\/vdfXvyl4_normal.png","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/693388490642423808\/vdfXvyl4_normal.png","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/842072275\/1410269304","profile_link_color":"ED7811","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"F6FFD1","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":true,"follow_request_sent":false,"notifications":false}]' }

How do I extract only the followers_count key value pair. As this is the only part I need. I may choose others after but this is the main part.
I have tried doing something like:

console.log(response.body['followers_count']);

But that doesnt work.

2

Answers


  1. Short answer is:

    var responseObject = JSON.parse(response);
    console.log(responseObject[0].followers_count)
    

    Mind you, the string you pasted above as response is not a valid JSON but I assume it’s an errore in copy / paste. You can use an online json validator to test it.

    Login or Signup to reply.
  2. response[0].followers_count; this will work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search