Mote Zart
Mote Zart

Reputation: 960

Getting response data out of Fetch blob from Wikipedia api

I'm trying to use Wikipedia's api with fetch.

const endpoint = 'https://en.wikipedia.org/w/api.php?
action=query&format=json&origin=*&titles=Albert%20Einstein'

 fetch(endpoint)
          .then(blob => blob.json())
          .then(data => console.log(data))

RETURNS

enter image description here

I'm not sure how to drill down in this, due to that 736. I assume there is nothing there? What do these results mean?

I did these loops to see if they allowed me something I could not get by clicking in the console. It's the same as fetch call above so not really useful. Just wanted to show that I tried something at least (downvote control).

fetch(endpoint)
    .then(blob => blob.json())
    .then((data) => {
    for(var i in data){
     //returns batch complete ,
      //& query
      for(var j in data.query){ 
         //returns pages
        for(var k in data.query.pages){
        //returns the string 736
        }
      }
  }
  })

Does nothing more than clicking the console results above.

Upvotes: 0

Views: 753

Answers (1)

Tgr
Tgr

Reputation: 28160

Use formatversion=2 for a slightly more comfortable format (where data.query.pages is an array).

Alternatively, you could iterate the object with something like

var pages = data.query.pages;
for (pageId in pages) {
    if (pages.hasOwnProperty(pageId)) {
        console.log(pages[pageId]);
    }
}

In modern browsers you can also use Object.values() to get the properties of the object in an array.

Upvotes: 2

Related Questions