How to get access to this nested object in json file when it has an ID

How to get access to this nested object in json file when it has an ID
0

#1

Hey guys,

When i want to build an Ajax callback function I have a problem:

How to access such a path:

"query": {
          "pages": {
                          "32908": {
                                           "pageid": 32908,
                                           "ns": 0,
                                           "title": "Warsaw",
                                            "index": 1,
                                           "extract": "Warsaw (Polish: Warszawa [varˈʂava]; see also other names) is the capital and largest city of Poland.",

As you can see, there is an ID of the article “32908”. How can i display the wanted object if i don’t know what would be the ID of the typed query?

(link here: https://en.wikipedia.org/w/api.php?action=query&format=json&generator=search&gsrnamespace=0&gsrlimit=10&prop=extracts|pageimages&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&callback=JSON_CALLBACK&gsrsearch=warsaw)

Thanks for advice :slight_smile: Best regards,
Sebastian


#2

Hey, here is the code I used:
$.each(dataReturnedByAPI.query.pages, function(index, value){ var title = value.title; }


#3

Yeah but using $.each in “pages” object i would get the stuff i don’t want too, like “pageid” or “ns”. How to choose only “title” and “extract” without all the rest in this example


#4

$.each loops through all the objects in query.pages. You can just take out the things you need. So if you need the title and extract of every object, I do think this is the best way.

Alternatively, you can also use: Object.keys(dataReturnedByAPI.query.pages) which will return an array of al the indexes. You can than use: dataReturnedByAPI.query.pages[Object.keys(dataReturnedByAPI.query.pages)[0]].title to acces the title of the first object in query.pages.


#5

So i when i use $.each i should then use if statement to find the index i want (here it would be [2] and [4]) ?

I made something like this, but it doesn’t seem to work. Not sure if the ajax part is wrongly written:

// AJAX 
function getArticles() {
  if (value != "") {
    $.getJSON(mainLink, function (response) {
        var articlesList = "<ul>";
      $.each(response.query.pages, function(index, article){
       
       if (index===2) {
          articlesList += "<li>" + "<h3>" + article.title + "</h3>";
       }
      
        if (index===4) {
          articlesList += "<p>" + article.extract +"</p>" + "</li>";
        }
      });
      articlesList += "</ul>";
    });
 
    $("section").html(articlesList);
  }

my codepen with it: https://codepen.io/RycerzPegaza/pen/EyKjJX?editors=0010


#6

The index in $.each is the name of the object, for example: “32908” and not the iteration count. You could add a variable count and count++ for each object. Than you could use: if(count === 2).

If you only need [2] and [4] you might be better of using the Object.keys() function.