How to find a Wikidata entity by property?

4.9k views Asked by At

I would like to know, is there a way to find a Wikidata entity by specified property using their API. For example, there are plenty entities that have Freebase ID property (Property:P646). It's unique identifier and I want to get an entity by this identifier.

Anyone know how to achieve this?

3

There are 3 answers

2
maxlath On BEST ANSWER

[updated answer: using the SPARQL endpoint]

wdq is being replaced by an official SPARQL endpoint, where this query looks like this:

PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT ?item ?itemLabel WHERE {
  ?item wdt:P646 "/m/0gnfq" .
}

you can try it at query.wikidata.org

and to get this query's results in JSON, you can make a request at "https://query.wikidata.org/sparql?format=json&query=YOURQUERY" where YOURQUERY is the URI-encoded SPARQL query:

https://query.wikidata.org/sparql?format=json&query=PREFIX%20wikibase%3A%20%3Chttp%3A%2F%2Fwikiba.se%2Fontology%23%3E%20PREFIX%20wdt%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F%3E%20SELECT%20%3Fitem%20%3FitemLabel%20WHERE%20%7B%20%3Fitem%20wdt%3AP646%20%22%2Fm%2F0gnfq%22%20.%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22%20.%20%7D%20%7D

[old answer: using WDQ]

you can fetch the wmflabs API (documentation) like this:

http://wdq.wmflabs.org/api?q=string[646:/m/0gnfq]

Here, I query the wikidata entities with Property 646 of value /m/0gnfq.

The answer will look like this:

{
  "status": {
    "error": "OK",
    "items": 1,
    "querytime": "161ms",
    "parsed_query": "STRING[646:'/m/0gnfq']"
  },
  "items": [
    180736
  ]
}

The entity I was looking for is thus Q180736.

Here, the query uses the string parameter as freebase IDs are strings in wikidata, but for properties implying wikidata entities as value type, you will need to use the claim parameter. An example from the documentation:

claim[138:676555] returns all items that are named after (P138) Francis of Assisi (Q676555).

0
Termininja On

I also prefer Wikidata Query API, but if you need some more information about the items you can use Wikidata API. For example, in your case, all items that link to Freebase identifier (P646) property are:

https://www.wikidata.org/w/api.php?action=query&format=xml&generator=backlinks&gblnamespace=0&gbllimit=5000&prop=info&gbltitle=Property:P646
0
Finn Årup Nielsen On

There is now a SPARQL endpoint at https://query.wikidata.org

There you can type in your SPARQL query for Wikidata. e.g., for the Freebase property it could look like this:

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX v: <http://www.wikidata.org/prop/statement/>
PREFIX q: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?subject ?subjectLabel WHERE {
  ?subject wdt:P646 ?object .

  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }
} LIMIT 10