Using Wiki API by format JSON can't get the content

I use this code to gathering the information from Wiki:

And I can get a JSON String like this

    "query": {
        "normalized": [{
            "from": "apple",
            "to": "Apple"
        "pages": {
            "18978754": {
                "pageid": 18978754,
                "ns": 0,
                "title": "Apple",
                "revisions": [{
                    "*": "Something....."

I can eval it to JSON, but the problem is, I can get into the query>pages, after that I can't get deeper, it was Because the Wiki API return me as a String 18978754, but it can't get the value by this:


Some assumption I need to clarify, I don't know the number 18978754. Do I need to get the number first or I can still get "Something..." within knowing the number.



What about using array-syntax :


Seems to be working, using firebug :

>>> data.query.pages[18978754]
Object pageid=18978754 ns=0 title=Apple revisions=[1]

And :

>>> data.query.pages[18978754].title

Note accessing data-object with an array-syntax is also possible for the other properties ; for instance :

>>> data['query'].pages[18978754].title

That's perfectly valid JS syntax :-)

Added after seing the comment / edit

If you don't know the ids of the pages, you can iterate over the pages, with something like this :

for (var pageId in data.query.pages) {
    if (data.query.pages.hasOwnProperty(pageId)) {

Note that I'm using hasOwnProperty to be sure the object I'm on has the property, and that it's not coming from any kind of inheritance or anything like that :

Every object descended from Object inherits the hasOwnProperty method. This method can be used to determine whether an object has the specified property as a direct property of that object; unlike the in operator, this method does not check down the object's prototype chain.

Depending on what's in "revision", you might have to do the same on that one too, btw...

Hope this helps better :-)

Second edit, after second set of comments :

Well, going a bit farther (didn't think you meant it literally) :


is an array (note the [] symbols) that seems to be able to contain several objects.
so, you can get the first one of those this way :


The second one this way :


(there is no second one in the example you provided, btw -- so this is in theory ^^ )

And so on.

To get everyone of those objects, you'd have to do some kind of loop, like this :

var num_revisions = data.query.pages[pageId].revisions.length;
var i;
for (i=0 ; i<num_revisions ; i++) {

And now, inside that loop, you should be able to get the '*' property of the given object :


So, the final code becomes :

for (var pageId in data.query.pages) {
    if (data.query.pages.hasOwnProperty(pageId)) {
        var num_revisions = data.query.pages[pageId].revisions.length;
        var i;
        for (i=0 ; i<num_revisions ; i++) {

Using this code in firebug, I now get the literral sting you're looking for :


Of course, you could probably just use :

for (var pageId in data.query.pages) {
    if (data.query.pages.hasOwnProperty(pageId)) {

Which will work fine if you always want to deal with only the first element of the revisions array.

Just beware : in your example, there was only one revision ; the code I provided should be able to deal with many ; up to you to determine what you want to do with those ;-)


Late but maybe helpful for someone else.

When you add &indexpageids to your request you will get the pageids. More information here.


I had this same issue while creating a php-based parser for the Wiki API - here's how I solved it.

$new_result = array_pop($result["query"]["pages"]);

$final_result = $new_result["pageid"];

Note: I know this is PHP code in a JS question, but couldn't find exactly the same question/problem in that forum - I'm sure this will be useful for someone.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.