Wikipedia API stuck retrieving info

Wikipedia API stuck retrieving info
0.0 0

#1

Hey,

here’s the pen - https://codepen.io/balabis/pen/LQJyPj

I am stuck on getting the information from the call.
Could someone please get me on the right tracks here ? :slight_smile:

If you would open the url ajax is calling you would see all the info there. I seem to have no problem accessing it until one point. I use data.query.pages to get to as low as pages. And then I’m stuck. Tried to get it by index but it won’t work. pages[1] does not get me results. I need to get all the data inside of those pages and cannot find a way. Eventually I will have to iterate through all of them and add the info to separate <li> elements.


#2

I do not see where you are trying to assign anything from the data.

If I search for “Free Code Camp”, data is an object that looks like the following. If I want to refer to the second page (pageid of 422887), then I would write:

var secondPage = data.query.pages.422887.title; // "Labor camp"

You will need to iterate through the pages object and use it’s keys (the pageids). Have a look at the Object.keys method for how to get an array of an object’s property names which you could then use to access each object’s values (title, extract, index (the order)).

{
  "batchcomplete": "",
  "continue": {
    "gsroffset": 10,
    "continue": "gsroffset||"
  },
  "query": {
    "pages": {
      "11227": {
        "pageid": 11227,
        "ns": 0,
        "title": "Freemasonry",
        "index": 4,
        "extract": "Freemasonry or Masonry consists of fraternal organisations that trace their origins to the local fraternities of stonemasons, which from the end of the fourteenth century regulated the qualifications of stonemasons and their interaction with authorities and clients."
      },
      "422887": {
        "pageid": 422887,
        "ns": 0,
        "title": "Labor camp",
        "index": 8,
        "extract": "A labor camp (or labour, see spelling differences) or work camp is a simplified detention facility where inmates are forced to engage in penal labor as a form of punishment under the criminal code."
      },
      "711372": {
        "pageid": 711372,
        "ns": 0,
        "title": "Last-call return",
        "index": 7,
        "extract": "Last-call return, automatic recall, or (on PBX and centrex service) camp-on, is a telecommunication feature offered by telephony service providers to subscribers to provide the subscriber with the telephone number, and sometimes the time, of the last caller."
      },
      "1178134": {
        "pageid": 1178134,
        "ns": 0,
        "title": "Black Codes (United States)",
        "index": 10,
        "extract": "In the United States, the Black Codes were laws passed by Southern states in 1865 and 1866."
      },
      "2775954": {
        "pageid": 2775954,
        "ns": 0,
        "title": "Code of the United States Fighting Force",
        "index": 9,
        "extract": "The Code of the U.S. Fighting Force is a code of conduct that is an ethics guide and a United States Department of Defense directive consisting of six articles to members of the United States Armed Forces, addressing how they should act in combat when they must evade capture, resist while a prisoner or escape from the enemy."
      },
      "2953900": {
        "pageid": 2953900,
        "ns": 0,
        "title": "Alternative terms for free software",
        "index": 6,
        "extract": "Alternative terms for free software, such as open source, FOSS, and FLOSS, have been a controversial issue among free and open-source software users from the late 1990s onwards."
      },
      "21139803": {
        "pageid": 21139803,
        "ns": 0,
        "title": "Code Camp",
        "index": 2,
        "extract": "Code Camp is a form of an unconference."
      },
      "36548336": {
        "pageid": 36548336,
        "ns": 0,
        "title": "Codecademy",
        "index": 5,
        "extract": "Codecademy is an online freemium interactive platform that offers free coding classes in 12 different programming languages including Python, Java, JavaScript (jQuery, AngularJS, React.js), Ruby, SQL, and Sass, as well as markup languages HTML and CSS. The site also offers a paid \"pro\" option that gives users access to a personalized learning plan, quizzes, realistic projects, and live help from advisors."
      },
      "49858916": {
        "pageid": 49858916,
        "ns": 0,
        "title": "Coding bootcamp",
        "index": 3,
        "extract": "Coding Bootcamps are short (usually 8- to 12-week) programs offered by private companies that teach students computer programming in a short period of time."
      },
      "52809488": {
        "pageid": 52809488,
        "ns": 0,
        "title": "FreeCodeCamp",
        "index": 1,
        "extract": "freeCodeCamp (also referred to as “Free Code Camp”) is a nonprofit organization that consists of an interactive learning web platform, an online community forum, chat rooms, Medium publications, and local organizations that intend to make learning web development accessible to anyone."
      }
    }
  },
  "limits": {
    "extracts": 20
  }
}