0

Let's say I have a JSON that looks like this:

[{
    "created_at": "Sun Apr 09 17:56:20 +0000 2017",
    "id": 851131679167139842,
    "id_str": "851131679167139842",
    "text": "hello world with an image goes here as a test! https:\/\/t.co\/jNfdESxPcn",
    "truncated": false,
    "entities": {
        "hashtags": [],
        "symbols": [],
        "user_mentions": [],
        "urls": [],
        "media": [{
            "id": 851131665623732225,
            "id_str": "851131665623732225",
            "indices": [47, 70],
            "media_url": "http:\/\/pbs.twimg.com\/media\/C8_TvYqW0AEbBaP.jpg",
            "media_url_https": "https:\/\/pbs.twimg.com\/media\/C8_TvYqW0AEbBaP.jpg",
            "url": "https:\/\/t.co\/jNfdESxPcn",
            "display_url": "pic.twitter.com\/jNfdESxPcn",
            "expanded_url": "https:\/\/twitter.com\/DavidHoperz\/status\/851131679167139842\/photo\/1",
            "type": "photo",
            "sizes": {
                "thumb": {
                    "w": 150,
                    "h": 150,
                    "resize": "crop"
                },
                "large": {
                    "w": 670,
                    "h": 728,
                    "resize": "fit"
                },
                "small": {
                    "w": 626,
                    "h": 680,
                    "resize": "fit"
                },
                "medium": {
                    "w": 670,
                    "h": 728,
                    "resize": "fit"
                }
            }
        }]
    },
    "extended_entities": {
        "media": [{
            "id": 851131665623732225,
            "id_str": "851131665623732225",
            "indices": [47, 70],
            "media_url": "http:\/\/pbs.twimg.com\/media\/C8_TvYqW0AEbBaP.jpg",
            "media_url_https": "https:\/\/pbs.twimg.com\/media\/C8_TvYqW0AEbBaP.jpg",
            "url": "https:\/\/t.co\/jNfdESxPcn",
            "display_url": "pic.twitter.com\/jNfdESxPcn",
            "expanded_url": "https:\/\/twitter.com\/DavidHoperz\/status\/851131679167139842\/photo\/1",
            "type": "photo",
            "sizes": {
                "thumb": {
                    "w": 150,
                    "h": 150,
                    "resize": "crop"
                },
                "large": {
                    "w": 670,
                    "h": 728,
                    "resize": "fit"
                },
                "small": {
                    "w": 626,
                    "h": 680,
                    "resize": "fit"
                },
                "medium": {
                    "w": 670,
                    "h": 728,
                    "resize": "fit"
                }
            }
        }]
    },
    "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,
    "user": {
        "id": 851119985732800513,
        "id_str": "851119985732800513",
        "name": "David",
        "screen_name": "DavidHoperz",
        "location": "",
        "description": "",
        "url": null,
        "entities": {
            "description": {
                "urls": []
            }
        },
        "protected": false,
        "followers_count": 0,
        "friends_count": 21,
        "listed_count": 0,
        "created_at": "Sun Apr 09 17:09:52 +0000 2017",
        "favourites_count": 0,
        "utc_offset": null,
        "time_zone": null,
        "geo_enabled": false,
        "verified": false,
        "statuses_count": 3,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "is_translation_enabled": false,
        "profile_background_color": "F5F8FA",
        "profile_background_image_url": null,
        "profile_background_image_url_https": null,
        "profile_background_tile": false,
        "profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
        "profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
        "profile_link_color": "1DA1F2",
        "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": true,
        "default_profile_image": false,
        "following": false,
        "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": 0,
    "favorited": false,
    "retweeted": false,
    "possibly_sensitive": false,
    "lang": "en"
}, {
    "created_at": "Sun Apr 09 17:47:36 +0000 2017",
    "id": 851129482832470026,
    "id_str": "851129482832470026",
    "text": "How to display twitter posts using javascript https:\/\/t.co\/I49vjVbAUJ",
    "truncated": false,
    "entities": {
        "hashtags": [],
        "symbols": [],
        "user_mentions": [],
        "urls": [{
            "url": "https:\/\/t.co\/I49vjVbAUJ",
            "expanded_url": "https:\/\/www.script-tutorials.com\/how-to-display-twitter-posts-using-javascript\/",
            "display_url": "script-tutorials.com\/how-to-display\u2026",
            "indices": [46, 69]
        }]
    },
    "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,
    "user": {
        "id": 851119985732800513,
        "id_str": "851119985732800513",
        "name": "David",
        "screen_name": "DavidHoperz",
        "location": "",
        "description": "",
        "url": null,
        "entities": {
            "description": {
                "urls": []
            }
        },
        "protected": false,
        "followers_count": 0,
        "friends_count": 21,
        "listed_count": 0,
        "created_at": "Sun Apr 09 17:09:52 +0000 2017",
        "favourites_count": 0,
        "utc_offset": null,
        "time_zone": null,
        "geo_enabled": false,
        "verified": false,
        "statuses_count": 3,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "is_translation_enabled": false,
        "profile_background_color": "F5F8FA",
        "profile_background_image_url": null,
        "profile_background_image_url_https": null,
        "profile_background_tile": false,
        "profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
        "profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
        "profile_link_color": "1DA1F2",
        "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": true,
        "default_profile_image": false,
        "following": false,
        "follow_request_sent": false,
        "notifications": false,
        "translator_type": "none"
    },
    "geo": null,
    "coordinates": null,
    "place": null,
    "contributors": null,
    "is_quote_status": false,
    "retweet_count": 2,
    "favorite_count": 1,
    "favorited": false,
    "retweeted": false,
    "possibly_sensitive": false,
    "lang": "en"
}, {
    "created_at": "Sun Apr 09 17:33:43 +0000 2017",
    "id": 851125986976567297,
    "id_str": "851125986976567297",
    "text": "Hello world",
    "truncated": false,
    "entities": {
        "hashtags": [],
        "symbols": [],
        "user_mentions": [],
        "urls": []
    },
    "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,
    "user": {
        "id": 851119985732800513,
        "id_str": "851119985732800513",
        "name": "David",
        "screen_name": "DavidHoperz",
        "location": "",
        "description": "",
        "url": null,
        "entities": {
            "description": {
                "urls": []
            }
        },
        "protected": false,
        "followers_count": 0,
        "friends_count": 21,
        "listed_count": 0,
        "created_at": "Sun Apr 09 17:09:52 +0000 2017",
        "favourites_count": 0,
        "utc_offset": null,
        "time_zone": null,
        "geo_enabled": false,
        "verified": false,
        "statuses_count": 3,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "is_translation_enabled": false,
        "profile_background_color": "F5F8FA",
        "profile_background_image_url": null,
        "profile_background_image_url_https": null,
        "profile_background_tile": false,
        "profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
        "profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
        "profile_link_color": "1DA1F2",
        "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": true,
        "default_profile_image": false,
        "following": false,
        "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": 0,
    "favorited": false,
    "retweeted": false,
    "lang": "en"
}]

And I have a jQuery JSON that looks like this:

var poutput = $('.listHolder');

$.ajax({
    url: 'page.php',
    dataType: 'json',
    timeout: 5000,
    success: function(data){
        $.each(data, function(pi,item){ 
            str = item.id_str;
            media = ""; 

            var mediaUrl = item.entities.media.media_url_https;
            alert(mediaUrl);
            var products = '<div id="'+item.id_str+'" class="items">'+
                '<p class="names">'+item.created_at+'</p>'+
                '<p class="names">'+item.text+'</p>'+
                media +
                '</div>';
            console.log(products);
            poutput.append(products);
        });
    },
    error: function(){
        //alert('There was an error loading the data.');
    }
});

I can easily get the created_at, text, and the id_str from that JSON but when I comes to getting the media_url or anything else, I always get an undefined error.

In my code above, I tried this:

var mediaUrl = item.entities.media.media_url_https;
alert(mediaUrl);

And it didn't work (undefined error).

I also tried:

var mediaUrl = item.entities.media_url_https;
alert(mediaUrl);

And still no luck.

Could someone please advise on this issue?

2
  • 3
    media is an array, so you need to access it by index, eg media[0].media_url_https Commented Apr 10, 2017 at 11:27
  • 2
    item.entities.media is not an OBJECT, it's an array of objects, so you need to specify the index (integer value starting from 0) from which member of the array you want to take the value of media_url_https. Commented Apr 10, 2017 at 11:27

1 Answer 1

3

Instead of this:

var mediaUrl = item.entities.media.media_url_https;

try this:

var mediaUrl = item.entities.media[0].media_url_https;

as media is an array and could contain more than 1 item, so you'll need to specify the index.

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

Comments

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.