3

I am busy with a angular 4.x app where I am trying to display a random wikipedia article. The JSON I see in chrome dev tools under query/pages alwasys has a different nubered pageID. Instead of returning pages as an array they return it as a JSON object which always has a different name so I'm not sure how I can turn that into a typescript interface or bind to it seeing as the name is aways different? Any idea how I can go about this?

the JSON I get from the wikipedia api:

{
  "batchcomplete": "",
  "continue": {
    "grncontinue": "0.241230031087|0.241230543855|19422120|0",
    "continue": "grncontinue||"
  },
  "warnings": {
    "extracts": {
      "*": "\"exlimit\" was too large for a whole article extracts request, lowered to 1."
    }
  },
  "query": {
    "pages": {
      "742585": {
        "pageid": 742585,
        "ns": 0,
        "title": "Speedway",
        "extract": "<p><b>Speedway</b> may refer to:</p>\n<h2><span id=\"In_racing\">In racing</span></h2>\n<ul><li>Oval track racing, motor racing on an oval track which turns in one direction</li>\n<li>Cycle speedway, a form of bicycle racing</li>\n<li>Motorcycle speedway, a form of motorcycle sport</li>\n<li>Dirt track racing, known as speedway in Australia and New Zealand</li>\n</ul><h2><span id=\"Other_uses\">Other uses</span></h2>\n<dl><dt>Placenames</dt>\n</dl><ul><li>Speedway, California, former town in Butte County</li>..."
      }
    }
  }
}
user2094257
  • 1,645
  • 4
  • 26
  • 53

3 Answers3

4

Use Object.keys to get the Keys inside the Object as Array

var data = {
  "batchcomplete": "",
  "continue": {
    "grncontinue": "0.241230031087|0.241230543855|19422120|0",
    "continue": "grncontinue||"
  },
  "warnings": {
    "extracts": {
      "*": "\"exlimit\" was too large for a whole article extracts request, lowered to 1."
    }
  },
  "query": {
    "pages": {
      "742585": {
        "pageid": 742585,
        "ns": 0,
        "title": "Speedway",
        "extract": "<p><b>Speedway</b> may refer to:</p>\n<h2><span id=\"In_racing\">In racing</span></h2>\n<ul><li>Oval track racing, motor racing on an oval track which turns in one direction</li>\n<li>Cycle speedway, a form of bicycle racing</li>\n<li>Motorcycle speedway, a form of motorcycle sport</li>\n<li>Dirt track racing, known as speedway in Australia and New Zealand</li>\n</ul><h2><span id=\"Other_uses\">Other uses</span></h2>\n<dl><dt>Placenames</dt>\n</dl><ul><li>Speedway, California, former town in Butte County</li>..."
      }
    }
  }
};

var MyArray = Object.keys(data.query.pages);

console.log('MyArray: '+ JSON.stringify(MyArray));

// Required Data

console.log('Page: '+ data.query.pages[MyArray[0]].pageid)
Sibiraj
  • 4,486
  • 7
  • 33
  • 57
1

You can use javascript Object.keys method something like

var obj={
  "batchcomplete": "",
  "continue": {
    "grncontinue": "0.241230031087|0.241230543855|19422120|0",
    "continue": "grncontinue||"
  },
  "warnings": {
    "extracts": {
      "*": "\"exlimit\" was too large for a whole article extracts request, lowered to 1."
    }
  },
  "query": {
    "pages": {
      "742585": {
        "pageid": 742585,
        "ns": 0,
        "title": "Speedway",
        "extract": "<p><b>Speedway</b> may refer to:</p>\n<h2><span id=\"In_racing\">In racing</span></h2>\n<ul><li>Oval track racing, motor racing on an oval track which turns in one direction</li>\n<li>Cycle speedway, a form of bicycle racing</li>\n<li>Motorcycle speedway, a form of motorcycle sport</li>\n<li>Dirt track racing, known as speedway in Australia and New Zealand</li>\n</ul><h2><span id=\"Other_uses\">Other uses</span></h2>\n<dl><dt>Placenames</dt>\n</dl><ul><li>Speedway, California, former town in Butte County</li>..."
      }
    }
  }
}

var pageNo=Object.keys(obj.query.pages)[0];

console.log(pageNo)
jitender
  • 10,238
  • 1
  • 18
  • 44
0

Use formatversion=2:

action=query&format=json&prop=extracts&exlimit=1&generator=random&formatversion=2

{
    "batchcomplete": true,
    "continue": {
        "grncontinue": "0.451777061970|0.451777269201|12035865|0",
        "continue": "grncontinue||"
    },
    "query": {
        "pages": [
            {
                "pageid": 18652441,
                "ns": 0,
                "title": "Kuczyny",
                "extract": "<p><b>Kuczyny</b> <span>[kuˈt͡ʂɨnɨ]</span> is a village in the administrative district of Gmina Stawiski, within Kolno County, Podlaskie Voivodeship, in north-eastern Poland. It lies approximately 4 kilometres (2 mi) north of Stawiski, 15 km (9 mi) east of Kolno, and 75 km (47 mi) north-west of the regional capital Białystok.</p>\n<p>The village has a population of 31.</p>\n<h2><span id=\"References\">References</span></h2>\n\n<p><br></p>\n\n<p><span></span></p>"
            }
        ]
    }
}
Tgr
  • 27,442
  • 12
  • 81
  • 118