Home > Software engineering >  UrlFetchApp returns no data from Wikidata SPARQL service
UrlFetchApp returns no data from Wikidata SPARQL service

Time:07-30

I'm trying to use Google Apps Script contained in a sheet to return XML from the Wikidata web service for SPARQL queries. I have a simple working SPARQL query and am using the URL-escaped https string, per the instructions in the Wikidata user guide.

Per the code below, I've tried UrlFetchApp.Fetch() both with and without the options parameter, but I always get an empty response. When I test the "simpleQry" url in my browser, it returns the correct xml file. I don't use web services very often, so I may be missing something obvious. Any help is greatly appreciated!

First I set the fetch options with JSON

 //set http header values w/json
var headr = {
    'Accept': 'application/sparql-results xml'
};

//set options values w/json
var options = {
    'method': 'get',
    'header': headr
};

Then I submit the request:

simpleQry = "https://query.wikidata.org/sparql?query=select ?personLabel ?employerLabel ?worksLabel
where {
  VALUES ?person {wd:Q112129152} .
  ?person wdt:P108 ?employer .
  ?person wdt:P800 ?works .
  SERVICE wikibase:label { bd:serviceParam wikibase:language '[AUTO_LANGUAGE],en'. }
  }";

var response = UrlFetchApp.fetch(simpleQry, options);
var dataAllxml = XmlService.parse(response.getContentText());

Process the results. This where the error gets uncovered. Length of content = 0.

var rows = [], data;

for (i = 0; i < dataAllxml.length; i  ) {
    data = dataAllxml[i];
    rows.push([data.personLabel, data.employerLabel, data.worksLabel]); //
}

CodePudding user response:

Modification points:

  • XmlService.parse(response.getContentText()) returns XML Object. By this, dataAllxml.length becomes undefined. By this, the for loop is not run. I think that this is the reason for your issue of Process the results. This where the error gets uncovered. Length of content = 0..

When this is reflected in your script, it becomes as follows.

Modified script:

function myFunction() {
  var headr = {
    'Accept': 'application/sparql-results xml'
  };
  var options = {
    'method': 'get',
    'header': headr
  };
  var simpleQry = "https://query.wikidata.org/sparql?query=select ?personLabel ?employerLabel ?worksLabel
where {
  VALUES ?person {wd:Q112129152} .
  ?person wdt:P108 ?employer .
  ?person wdt:P800 ?works .
  SERVICE wikibase:label { bd:serviceParam wikibase:language '[AUTO_LANGUAGE],en'. }
  }";
  var response = UrlFetchApp.fetch(simpleQry, options);
  var dataAllxml = XmlService.parse(response.getContentText());

  // I modified below script.
  var root = dataAllxml.getRootElement();
  var ns = root.getNamespace();
  var rows = root.getChild("results", ns).getChildren().map(e => e.getChildren().map(f => f.getValue().trim()));
  console.log(rows);
}

Testing:

When this modified script is run, the following result is obtained.

[
  ["Ramona L. Pérez","San Diego State University","Good and bad death: exploring the perspectives of older Mexican Americans."],
  ["Ramona L. Pérez","San Diego State University","Diabetes Cultural Beliefs and Traditional Medicine Use Among Health Center Patients in Oaxaca, Mexico."],
  ["Ramona L. Pérez","San Diego State University","Exposure to smoking in soap operas and movies: smoking cessation and attempts to quit."]
]

References:

CodePudding user response:

@Tanaike, your revision works like a charm, and integrated with my downstream code w/o modifications. I didn't realize that the .length property was inappropriate in this case. Thanks for the pointers on XmlService and map. Very educational.

  • Related