smihael smihael -5 years ago 109
JSON Question

How to get latest version of certain software using wikidata

I use following command to get (latest) version of certain software from Wikidata from the command line using

as json parser.

curl -sL "$QID&languages=en&format=json" |
jq ".entities.$$PID""[0].mainsnak.datavalue.value"

is the ID of a Wikidata entry, and
the ID of the propperty we want to print (in this case software version "P348").

That usually works fine, since usually the first claim (
) for
is the newest version, but for example for "Q13166" (Wordpress), we have several claims about software version. How to get the newest stable one instead of the first claim?

I probably should (in the case we have more than one claim), find the ith claim, where version type "P548" equals to stable version "Q12355314". Or find out the clam that has preferred rank. How to do that with jq? Is there an easier way, such as for example sending an SPARQL query to

Answer Source

The following collects all the relevant version numbers, and determines the "maximum" value using the filter defined here as "lexmax":

jq --arg QID "$QID" --arg PID "$PID" '
  def lexmax:
    map( split(".")
         | map(if test("^[0-9]*$") then tonumber else . end) )
  | max | map(tostring) | join(".");

  .entities | .[$QID] | .claims | .[$PID]
  | map(.mainsnak.datavalue.value)
  | lexmax'

The result with QID=Q13166 PID=P348 is


If you want to use .rank == "preferred" as a selection criterion, you could use the filter:

.entities | .[$QID] | .claims | .[ $PID ]
| map( select(.rank == "preferred") | .mainsnak.datavalue.value)
| lexmax

Or perhaps you won't need to use lexmax ...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download