Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

327
Views
How can I access a JS object property whose value is an unknown integer?

The JSON response returned from this wikipedia API call is a series of nested objects. To travel down the object property chain and access the text of interest, I have to first access a property whose value is a random number, dependent upon the wikipedia page I query by title.

An example for the page titled "San%20Francisco" (page id = 49728):

Object Property Chain:

responseJSON.wiki[0].query.pages[<<page id>>].extract

Example API Call: https://en.wikipedia.org/w/api.php/?origin=*&format=json&action=query&prop=extracts&exintro=&explaintext=&titles=San%20Francisco

Is there some way that I can identify the property whose value is a random integer? There is only one child of pages in the chain whose value is an integer. I cannot think of another solution and do not know of any JSON parsing method that would be effective.

I am inexperienced with AJAX requests and am making my ajax call in this way using jQuery. I would like to mention this in case I am doing something naive:

var getWiki = function (obj) {
    return $.ajax({
        url: "http://en.wikipedia.org/w/api.php" +
            "?origin=*" + "&format=json" +
            "&action=query" + "&prop=extracts" +
            "&exintro=" + "&explaintext=" + "&titles=" +
            obj.position,
        method: 'GET'
    });
};
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

If your object has only a single key, you can get it using Object.keys(object)[0] and then perform a dynamic bracket-notation property access on the original object. (This is what the dig utility does in the example below.)

Also note that you can use .promise() to make handling your JSON response a bit tidier. I would suggest you add type: 'json' to your AJAX request, too, so that you don't have to parse the string data yourself.

function getWiki(obj) {
  return $.ajax({
    url: "http://en.wikipedia.org/w/api.php" +
      "?origin=*" + "&format=json" +
      "&action=query" + "&prop=extracts" +
      "&exintro=" + "&explaintext=" + "&titles=" +
      obj.position,
    method: 'GET',
    type: 'json'
  }).promise()
}


function dig(object) {
  return object[Object.keys(object)[0]]
}


getWiki({
    position: 'San Francisco'
  })
  .then(function(json) {
    console.log(
      dig(json.query.pages).extract //=> 'San Francisco (SF) ...'
    )
  })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

about 4 years ago · Santiago Trujillo Report

0

Well, if there is always a single property on pages object then you can try either method:

if (typeof Object.values !== 'function') {
  Object.values = obj => Object.keys(obj).map(key => obj[key]);
}

const responseJSON = {
  "batchcomplete": "",
  "query": {
    "pages": {
      "49728": {
        "pageid": 49728,
        "ns": 0,
        "title": "San Francisco",
        "extract": "Some text"
      }
    }
  }
}

const pages = responseJSON.query.pages;

const extractedWithKeys = pages[Object.keys(pages)[0]];
const extractedObjValues = Object.values(pages)[0];

console.log(extractedWithKeys, extractedObjValues)

about 4 years ago · Santiago Trujillo Report

0

You can actually do this without Object.keys or other similar tricks if you add the indexpageids parameter to the query.

Example API call: https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&indexpageids=1&titles=San+Francisco&exintro=1&explaintext=1

Example JSON output:

{
    "batchcomplete": "",
    "query": {
        "pageids": [
            "49728"
        ],
        "pages": {
            "49728": {
                "pageid": 49728,
                "ns": 0,
                "title": "San Francisco",
                "extract": "San Francisco (initials SF) <snip>"
            }
        }
    }
}

Then you can use something like data.query.pages[data.query.pageids[0]].extract to get the extract (although bear in mind that sometimes no pages may be returned, depending on what query you use).

Wikipedia's API sandbox is useful for discovering parameters like indexpageids - experimenting there is usually quicker than reading the docs.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!