wikidata-api

Query Wikidata REST API with related identifier


I am attempting to do alignments for a set of known VIAF IDs. I would like to query the Wikidata REST API with a VIAF ID (P214) and get back a set of one or more Wikidata entity IDs (QXXXXX) that correspond to that VIAF entity. I am unable to find any examples of this either in the Wikidata API documentation or otherwise online.

I've noodled around with various permutations of queries using action=wbsearchentities and action=query, all to no avail.

Could anyone kindly point me to set of docs or example code that enumerates the correct query parameters for such a search?


Solution

  • Let's suppose you want to find the item whose VIAF ID is "113230702" (i.e. Douglas Adams).

    Solution 1

    Use action=query:

    https://www.wikidata.org/w/api.php?action=query&format=json&list=search&srsearch=haswbstatement:P214=113230702
    

    URL response:

    {"batchcomplete":"","query":{"searchinfo":{"totalhits":1},"search":[{"ns":0,"title":"Q42","pageid":138,"size":319024,"wordcount":1204,"snippet":"Douglas Adams\nDouglas Adams\n\u0414\u0443\u0433\u043b\u0430\u0441 \u0410\u0434\u0430\u043c\u0441\nDouglas Adams\nDouglas Adams\nDouglas Adams\nDouglas Adams\nDouglas Adams\nDouglas Adams\nDouglas Adams\nDouglas Adams","timestamp":"2021-08-13T22:04:39Z"}]}}
    

    Solution 2

    Use Wikidata Query Service:

    https://query.wikidata.org/bigdata/namespace/wdq/sparql?format=json&query=SELECT%20DISTINCT%20%3Fx%0AWHERE%20{%0A%20%20%3Fx%20wdt%3AP214%20%22113230702%22%0A}
    

    This last URL comes from the following SPARQL query:

    SELECT DISTINCT ?x
    WHERE {
      ?x wdt:P214 "113230702"
    }
    

    URL response:

    {
      "head" : {
        "vars" : [ "x" ]
      },
      "results" : {
        "bindings" : [ {
          "x" : {
            "type" : "uri",
            "value" : "http://www.wikidata.org/entity/Q42"
          }
        } ]
      }
    }