Example of Pulling of Structured Data from Wikidata.org

David Janes
2 min readDec 17, 2020

Wikidata is a free and open knowledge base that can be read and edited by both humans and machines. Wikidata acts as central storage for the structured data of its Wikimedia sister projects including Wikipedia, Wikivoyage, Wiktionary, Wikisource, and others. It shares data with Wikipedia; from a programmer’s perspective, it’s a lot easier to get data from Wikidata — once you know the tricks.

When you see “Structured” or “Linked” data, it won’t be unexpected to see in the mix SPARQL, a WC3 standard query language for this type of data. SPARQL isn’t the easiest thing in the world to use, so usually what I do is start with an example and mutate it until I get the results I’m expecting.

Here’s an example query that will return the names of all Transport Companies and their logos.

How did I come up with wdt:P31, wd:Q740752 and wdt:P154? By looking for things on Wikidata (for example, the TTC) and clicking on links. The magic codes you need are listed on each page. Note that (basically speaking) “wdt:P###” describes relationships between “wd:Q###” things.

My example is pretty simple, but Wikidata gives you a lot more example queries to work with with far more complicated queries. If you need to do it from code (develop your query online first), it’s a simple HTTP GET with the query encoded in the params.

--

--