2

I'm currently using a twitter API (twit npm package) and I am currently getting a response with the structure like so

{ metadata: [Object],                                                                                                                                                                         
I20150213-22:24:29.155(0)?        created_at: 'Fri Feb 13 22:24:07 +0000 2015',                                                                                                                                               
I20150213-22:24:29.156(0)?        id: 566362206468845600,                                                                                                                                                                     
I20150213-22:24:29.156(0)?        id_str: '566362206468845569',                                                                                                                                                               
I20150213-22:24:29.157(0)?        text: 'Photo: New BTS picture from Mockingjay part  http://t.co/fo30mcLRYV',                                                                                                                
I20150213-22:24:29.158(0)?        source: '<a href="http://www.tumblr.com/" rel="nofollow">Tumblr</a>',                                                                                                                       
I20150213-22:24:29.158(0)?        truncated: false,                                                                                                                                                                           
I20150213-22:24:29.159(0)?        in_reply_to_status_id: null,                                                                                                                                                                
I20150213-22:24:29.159(0)?        in_reply_to_status_id_str: null,                                                                                                                                                            
I20150213-22:24:29.160(0)?        in_reply_to_user_id: null,                                                                                                                                                                  
I20150213-22:24:29.160(0)?        in_reply_to_user_id_str: null,                                                                                                                                                              
I20150213-22:24:29.161(0)?        in_reply_to_screen_name: null,                                                                                                                                                              
I20150213-22:24:29.161(0)?        user: [Object],                                                                                                                                                                             
I20150213-22:24:29.162(0)?        geo: null,                                                                                                                                                                                  
I20150213-22:24:29.162(0)?        coordinates: null,                                                                                                                                                                          
I20150213-22:24:29.163(0)?        place: null,                                                                                                                                                                                
I20150213-22:24:29.163(0)?        contributors: null,                                                                                                                                                                         
I20150213-22:24:29.164(0)?        retweet_count: 0,                                                                                                                                                                           
I20150213-22:24:29.288(0)?        favorite_count: 0,                                                                                                                                                                          
I20150213-22:24:29.289(0)?        entities: [Object],                                                                                                                                                                         
I20150213-22:24:29.289(0)?        favorited: false,                                                                                                                                                                           
I20150213-22:24:29.290(0)?        retweeted: false,                                                                                                                                                                           
I20150213-22:24:29.290(0)?        possibly_sensitive: false,                                                                                                                                                                  
I20150213-22:24:29.291(0)?        lang: 'en' },    

I am trying to retrieve the entities object to access the potential media items inside it however when I console.log() the entities key in this Array [object Object] gets logged. How do I access the inside of the object? I intend to extract a picture url from inside this object but I can't see whats inside. According to the Twitter API website media is stored inside entities objects.

Any help would be appreciated.

1
  • I think pretty much all browser consoles allow you to click onto such an [object Object] thingie to see actual property structure below it … Commented Feb 13, 2015 at 22:45

1 Answer 1

1

The URL's to the image is stored inside the entity media in the JSON response the REST call returns.

Say you want to access the media_url entity, simply access it as below

var result = the result from your call...
var imageUrl = result.entities.media.media_url;

and use the media URL in whatever way you want.

If you want to check the existence of media content, simply use if(result.entities.media.length > 0).

Sign up to request clarification or add additional context in comments.

3 Comments

And would there be a way to make sure that the tweet object has something inside media_url before assigning it to a variable? Because if it doesn't I'm getting an error back.
For some reason every time I try to log the media_url in the way you specified it still says its undefined. console.log(imageUrl.media_url); But it has no problem just outputting console.log(result.entities.media) and showing the media object. Do you have any idea why this is?
Two reasons may cause this; a) You have a typo somewhere in the variable names, or b) The media_url entity is undefined. Are you sure there actually is a media entity returned? What browser are you using? Why not just put a breakpoint in the code and check what the result returns?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.