Leth Leth - 11 months ago 44
JSON Question

Get an array of links from a specific wiki article

I am toying around with the wiki API and trying to have an array of links from a given article returned. This is the kind of JSON i am working with:

"continue": {
"plcontinue": "22989|0|2007_Rugby_World_Cup",
"continue": "||"
"query": {
"normalized": [
"from": "paris",
"to": "Paris"
"pages": {
"22989": {
"pageid": 22989,
"ns": 0,
"title": "Paris",
"links": [
"ns": 0,
"title": ", Île-de-France, Seine, Kingdom of France"

As you can see I need to know the pageId for me to access the links array that I need. I tried extracting it from the plcontinue value and then have it inserted later in my code, but it returns an undefined.

This is my API request:

url: "https://en.wikipedia.org/w/api.php?action=query&titles=" + searchTerm + "&pllimit=50&prop=links&format=json",
method: "GET",
dataType: "jsonp" //allow CORS
}).then(function (data) {
var plcontinueArray = data.continue.plcontinue.split("|")
var pageId = plcontinueArray[0];
var links = data.query.pages.pageId.links;

How do i access the array of links when i do not know the pageId before making the request?

Lix Lix
Answer Source

In the same way that you use dot notation to access the data in an object, you can also use an index like method to access keys of an object.

For example:

var obj = { foo: "bar", spam: "eggs" };
var value1 = obj.foo; // value1 = "bar"
var keyName = "spam";
var value2 = obj[keyName]; // value12 = "eggs"

So for your example, you would do something like this -

var pageId = plcontinueArray[0];
var links = data.query.pages[pageId].links;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download