skip to Main Content

I’m trying to get the lastest version of "spip"
For that, I can use a JSON output from https://www.spip.net/spip_loader.api but I don’t know how to get the 2nd record key (4.2.5) in bash with jq.

$ wget -q -O- https://www.spip.net/spip_loader.api | jq '.versions'
{
  "dev": "spip/dev/spip-master.zip",
  "4.2.5": "spip/archives/spip-v4.2.5.zip",
  "4.1.12": "spip/archives/spip-v4.1.12.zip",
  "4.0.11": "spip/archives/spip-v4.0.11.zip",
  "3.2.19": "spip/archives/spip-v3.2.19.zip"
}

I could just get 3rd line with | sed -n '3p' but I don’t find it very clean and I think that using jq completely would be more suitable.

I admit that it’s more out of curiosity than out of need since I can do otherwise.

2

Answers


  1. You can use keys_unsorted to get a list of keys in representation order, then take the second one (at index 1):

    jq -r '.versions | keys_unsorted[1]'
    

    Demo

    Getting "the lastest version" highly depends on how the version numbers are constructed. Your sample suggests that considering numbers only will suffice. So, strip consecutive digits from the keys, turn them into numbers, and get the maximum by that:

    jq -r '.versions | keys_unsorted | max_by([scan("\d+") | tonumber])'
    

    Demo

    Output:

    4.2.5
    
    Login or Signup to reply.
  2. This will produce both version and path :

    wget -q -O- https://www.spip.net/spip_loader.api |
      jq -r '.versions | to_entries
            | max_by(.key | [scan("\d+") | tonumber])
            | "(.key) (.value)"'
    # Output: 4.2.5 spip/archives/spip-v4.2.5.zip
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search