Ware Ware - 1 month ago 15
Java Question

How to get the Property Labels of an Wikidata Item?

How would you request the set of all Properties that an Item has?

Specifically, I'm looking for the English Labels for each Item that is the Value of an Item's Property.

E.g. "Earth":

{
"Challenger Deep",
"Solar System",
"oblate spheroid",
"geoid",
"World Ocean",
"Afro-Eurasia",
...
}


I'm using the Wikidata Toolkit library for now, but I'd take recommendations.

Answer

First you need to use MediaWiki API with action wbgetclaims to get all properties for each one claim. For example for Wikidata item Earth (Q2) the request will be:

https://www.wikidata.org/w/api.php?action=wbgetclaims&format=xml&props=value&entity=Q2

Then you need to parse the response and to take numeric-id from the value for each one property. In this example, for property P1589 the item numeric-id is 459173.

The last step is to create another request by using the same API with action wbgetentities:

https://www.wikidata.org/w/api.php?action=wbgetentities&props=labels&format=xml&languages=en&ids=Q459173|Q544|Q3241540|Q185969|Q715269|Q27527

where ids are the all numeric-ids from the previous step, prefixed with Q and separated by pipe |. This will return the all item labels in English:

<entities>
    <entity id="Q459173"><label language="en" value="Challenger Deep" /></entity>
    <entity id="Q544"><label language="en" value="Solar System" /></entity>
    <entity id="Q3241540"><label language="en" value="oblate spheroid" /></entity>
    <entity id="Q185969"><label language="en" value="geoid" /></entity>
    <entity id="Q715269"><label language="en" value="World Ocean" /></entity>
    <entity id="Q27527"><label language="en" value="Afro-Eurasia" /></entity>
    ...
<entities>