Javascript code to get the extract of wikipedia entry.

(Alexander Gozali) #1

Using REST API on wikipedia, i managed to get a wikipedia entry. It shows like this

  "statusCode": 200,
  "body": {
    "batchcomplete": "",
    "query": {
      "pages": {
        "547384": {
          "pageid": 547384,
          "ns": 0,
          "title": "Steven Gerrard",
          "extract": "Steven George Gerrard  (born 30 May 1980) is an English professional football manager and former player who manages Scottish Premiership club Rangers. -cont-

my problem is I want to fetch only the “extract” section but I can’t do that because there’s a number on the variable path, i tried

var extract = context.GetWikipediaEntry.response.body.query.pages.547384.extract

but it says error in 547384 with description “a leading decimal point can be confused with a dot”, any solution? thank you

How to search wikipedia simple entry?
(Karthik Tadikonda) #2

Hi @alexander.gozali,

You will have to use this way:

var extract = context.GetWikipediaEntry.response.body.query.pages[“547384”].extract

(Alexander Gozali) #3

hi Karthik, thanks for answering, I managed to get only the extract part using the method you told

My next question is, every wikipedia entry has their own page ID (in above example, 547384), and the page ID we get is different every time depends on what the user input is.

So what do I code in the script node so I can get the extract of the entries, because the page ID is different everytime, thank you

(Alexander Gozali) #4

Hi Karthik, thanks for answering, I managed to get only the extract part using your method

My next question is, every wikipedia page has a different pageID, and to access the “extract” part I need to access the pageid. And since every time an user search for a different entry, a different page ID will be use, so what to code in the script node? do I store the pageid in a variable? but I can’t figure it out how, or maybe there’s another way?

Thank you for your help.

(Karthik Tadikonda) #5

Hi @alexander.gozali,

Basically, you have to put some JS code to extract key from an array.

Use “Object.keys()” which actually returns array of keys. Iterate through those keys upon some condition and get subsequent child nodes