0

I have this Node.js sparql fetcher from Wikidata:

next()

async function next(offset = 0) {
  const sparql = `
  SELECT ?person ?pic ?givenName ?familyName ?placeOfBirth
  WHERE {
    ?person wdt:P31 wd:Q5 .
    ?person wdt:P18 ?pic .
    ?person wdt:P735 ?givenName .
    ?person wdt:P734 ?familyName .
    ?person wdt:P19 ?placeOfBirth .
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
  }
  OFFSET ${offset}
  LIMIT 50`
  const query = new URLSearchParams({ query: sparql }).toString();
  const url = ` https://query.wikidata.org/bigdata/namespace/wdq/sparql?${query}`
  console.log(url)
  const res = await fetch(url, { headers: { Accept: 'application/sparql-results+json' } })
  const json = await res.json()
  const array = json.results.bindings
  if (!array.length) {
    return
  }
  console.log(array)
}

However, it is returning an array of values like this:

{
  person: { type: 'uri', value: 'http://www.wikidata.org/entity/Q15198783' },
  placeOfBirth: { type: 'uri', value: 'http://www.wikidata.org/entity/Q61' },
  familyName: { type: 'uri', value: 'http://www.wikidata.org/entity/Q2658150' },
  pic: {
    type: 'uri',
    value: 'http://commons.wikimedia.org/wiki/Special:FilePath/Charles%20Baker%202016.jpg'
  },
  givenName: { type: 'uri', value: 'http://www.wikidata.org/entity/Q2958359' }
}

The pic is at least resolved, but everything else is not. How can I just get both the entity ID for each, as well as the resolved literal value for it, for all these properties?

1 Answer 1

1

You need to select additional variables on the pattern ${variableName}Label:

SELECT ?person ?personLabel ?pic ?givenName ?givenNameLabel ?familyName ?familyNameLabel ?placeOfBirth ?placeOfBirthLabel
WHERE {
  ?person wdt:P31 wd:Q5 .
  ?person wdt:P18 ?pic .
  ?person wdt:P735 ?givenName .
  ?person wdt:P734 ?familyName .
  ?person wdt:P19 ?placeOfBirth .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 50

See the User Manual for more details

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.