Bob Vandevliet
Bob Vandevliet

Reputation: 213

How to get only the value of the first data object from a JSON response when there are more data objects with the same name?

I want a value from a JSON request to be filled in an source url as in the code I currently have below:

function Source() {
$.ajax({
    type: "GET",
    dataType: "json",
    url: "https://api.hitbox.tv/media/video/chairstream/list?filter=recent",
    success: function(data) {
        var idvid = data.media_id;
        document.getElementById("latest").src = "http://www.hitbox.tv/#!/embedvideo/" + idvid() + "?autoplay=true";
    }
});

Problem is that the variable idvid isn't doing his work in the url. Another problem is that there are more data objects named "media_id" in the JSON response and I wan't to know how to get only the value of the first media_id. I'll be very happy when both problems could be solved.

Upvotes: 0

Views: 1513

Answers (2)

mplungjan
mplungjan

Reputation: 178385

I called the URL and got it using this

data.video[0].media_id

so (dropping the () from the var too as suggested by Andy):

success: function(data) {
    $("#latest").attr("src","http://www.hitbox.tv/#!/embedvideo/" + 
     data.video[0].media_id + 
     "?autoplay=true");
}

var data = {
  "request": {
    "this": "\/media\/video\/chairstream\/list"
  },
  "media_type": "video",
  "video": [{
    "media_user_name": "chairstream",
    "media_id": "783994",
    "media_file": "2c767c827c6c84a4714b9379983de69449c5a1c1-5658c80c2b938",
    "media_user_id": "871797",
    "media_profiles": "[{\"url\":\"\\\/chairstream\\\/2c767c827c6c84a4714b9379983de69449c5a1c1-5658c80c2b938\\\/chairstream\\\/index.m3u8\",\"height\":\"800\",\"bitrate\":0}]",
    "media_type_id": "2",
    "media_is_live": "1",
    "media_live_delay": "0",
    "media_date_added": "2015-11-27 23:16:04",
    "media_live_since": null,
    "media_transcoding": null,
    "media_chat_enabled": "1",
    "media_countries": null,
    "media_hosted_id": null,
    "media_mature": null,
    "media_hidden": null,
    "media_offline_id": null,
    "user_banned": null,
    "media_name": "2c767c827c6c84a4714b9379983de69449c5a1c1-5658c80c2b938",
    "media_display_name": "chairstream",
    "media_status": "Chairstream.com - Nov 27th #5",
    "media_title": "Chairstream.com - Nov 27th #5",
    "media_description": "",
    "media_description_md": null,
    "media_tags": "",
    "media_duration": "6262.0000",
    "media_bg_image": null,
    "media_views": "9",
    "media_views_daily": "2",
    "media_views_weekly": "9",
    "media_views_monthly": "9",
    "category_id": null,
    "category_name": null,
    "category_name_short": null,
    "category_seo_key": null,
    "category_viewers": null,
    "category_media_count": null,
    "category_channels": null,
    "category_logo_small": null,
    "category_logo_large": null,
    "category_updated": null,
    "team_name": null,
    "media_start_in_sec": "0",
    "media_duration_format": "01:44:22",
    "media_thumbnail": "\/static\/img\/media\/videos\/2c7\/2c767c827c6c84a4714b9379983de69449c5a1c1-5658c80c2b938_mid_000.jpg",
    "media_thumbnail_large": "\/static\/img\/media\/videos\/2c7\/2c767c827c6c84a4714b9379983de69449c5a1c1-5658c80c2b938_large_000.jpg",
    "channel": {
      "followers": "0",
      "user_id": "871797",
      "user_name": "chairstream",
      "user_status": "1",
      "user_logo": "\/static\/img\/channel\/chairstream_5602c1d59d02e_large.png",
      "user_cover": "\/static\/img\/channel\/cover_53e89056cf3ef.jpg",
      "user_logo_small": "\/static\/img\/channel\/chairstream_5602c1d59d02e_small.png",
      "user_partner": null,
      "partner_type": null,
      "user_beta_profile": "0",
      "media_is_live": "0",
      "media_live_since": "2015-11-30 12:35:33",
      "user_media_id": "206125",
      "twitter_account": null,
      "twitter_enabled": null,
      "livestream_count": "1"
    }
  }]
}

console.log(data.video[0].media_id)

Upvotes: 2

Andy
Andy

Reputation: 63589

Not sure why you have braces after idvid but it should just be

"http://www.hitbox.tv/#!/embedvideo/" + idvid + "?autoplay=true";

To get the media_id of the first object in the data array:

var idvid = data.video[0].media_id;

Upvotes: 3

Related Questions