Saturday, 8 April 2017

Access specific part of JSON twitter node module

I have done a request to get the tweets from a specified account using https://www.npmjs.com/package/twitter however, I cannot access the "text" part of the JSON object that is returned.

This is an example part of the JSON that is returned from the request:

[ { created_at: 'Sat Apr 08 10:16:51 +0000 2017',
    id: 850653659025334300,
    id_str: '850653659025334272',
    text: 'Who gets your pick on #GrandNational2017 day? šŸŽ\n\nOur Pinstickers\' guide to the 40-horse race\n\nšŸ¤”… ',
    truncated: true,
    entities:
     { hashtags: [Object],
       symbols: [],
       user_mentions: [],
       urls: [Object] },
    source: '<a href="http://www.socialflow.com" rel="nofollow">SocialFlow</a>',
    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,
    user:
     { id: 265902729,
       id_str: '265902729',
       name: 'BBC Sport',
       screen_name: 'BBCSport',
       location: 'MediaCityUK, Salford',
       description: 'Official account. Also from @bbc - @bbcmotd @bbcf1 @bbctms @bbctennis @bbcrugbyunion & @bbcgetinspired',
       url: '',
       entities: [Object],
       protected: false,
       followers_count: 6620652,
       friends_count: 264,
       listed_count: 17733,
       created_at: 'Mon Mar 14 09:44:40 +0000 2011',
       favourites_count: 161,
       utc_offset: -36000,
       time_zone: 'Hawaii',
       geo_enabled: true,
       verified: true,
       statuses_count: 263835,
       lang: 'en',
       contributors_enabled: false,
       is_translator: false,
       is_translation_enabled: true,
       profile_background_color: 'C0DEED',
       profile_background_image_url: 'http://pbs.twimg.com/profile_background_images/240463705/BBCSportTwitter_backing11.jpg',
       profile_background_image_url_https: 'https://pbs.twimg.com/profile_background_images/240463705/BBCSportTwitter_backing11.jpg',
       profile_background_tile: false,
       profile_image_url: 'http://pbs.twimg.com/profile_images/803606866974609412/Ymsnopmj_normal.jpg',
       profile_image_url_https: 'https://pbs.twimg.com/profile_images/803606866974609412/Ymsnopmj_normal.jpg',
       profile_banner_url: 'https://pbs.twimg.com/profile_banners/265902729/1490963959',
       profile_link_color: '0084B4',
       profile_sidebar_border_color: 'C0DEED',
       profile_sidebar_fill_color: 'DDEEF6',
       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,
       translator_type: 'none' },
    geo: null,
    coordinates: null,
    place: null,
    contributors: null,
    is_quote_status: false,
    retweet_count: 0,
    favorite_count: 6,
    favorited: false,
    retweeted: false,
    possibly_sensitive: false,
    lang: 'en' } ]

I am simply wanting to console.log the text: part of the JSON. Here is my code:

const Twitter = require('twitter');

const Twit = new Twitter({
    consumer_key: 'XXX',
    consumer_secret: 'XXX',
    access_token_key: 'XXX',
    access_token_secret: 'XXX'
});

Twit.get('statuses/user_timeline', { screen_name: 'BBCSport', count: 1 }, function(error, tweets, response) {
    if (error) {
        console.log(error)
    }
    console.log(tweets);
});

I have tried console.log(tweets.text) which I assumed would work, however I receive an error saying that it is undefined.



via jord

No comments:

Post a Comment