benjm
benjm

Reputation: 79

How to get the latest release from Github API using JSON

I have tried to get the latest release from github. I think I may be reading the array wrong. What am I doing wrong here?

    var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    var JS = JSON.parse(this.responseText);     
  console.log(JS.tag_name)
  
  
  }
};
xmlhttp.open("GET", "https://api.github.com/repos/NAME/REPO/releases", true);
xmlhttp.send();

With the response from the console:

undefined

I am new to JSON so I am a little confused on how to read an array.

Upvotes: 2

Views: 2858

Answers (1)

Allan Chain
Allan Chain

Reputation: 2815

/repos/NAME/REPO/releases returns an array of releases, and you want to get the first one with JS[0].tag_name.

If you just need the latest release, use /repos/NAME/REPO/releases/latest and keep JS.tag_name.

Upvotes: 4

Related Questions