Search code examples
jsonjqmaxby

Get newest Jar in Artifactory using JQ API


I need to download application JAR for execution, and I managed to figure it out up to this step part in my script:

jq '.files[] | select(.uri | contains("RELEASE") and contains(".jar"))'

This gives me a bunch of results that looks like multiple blocks of this:

{
  other versions
}
{
  "uri": "/2.0.6-RELEASE/app-name-2.0.6-RELEASE.jar",
  "size": 32981192,
  "lastModified": "2020-02-05T14:21:06.728-05:00",
  "folder": false,
  "sha1": "whatever",
  "sha2": "whatever2",
  "mdTimestamps": {
    "properties": "2020-02-05T14:21:13.468-05:00"
  }
}

Based on the documentation, I tried to extend this to:

jq '.files[] | select(.uri | contains("RELEASE") and contains(".jar")) | max_by(.lastModified)' But it returns error jq: error (at <stdin>:3284): Cannot index string with string "lastModified"

Documentation is vague on combining all the different search filters together.. I am not sure what is the correct syntax, please.

My goal: Retrieve only one element, ideally just the URI string so that I can use that to fetch the JAR directly on my next command without further processing.

I also tried the documented syntax for max_by with suffix: | {uri} - same error.


Solution

  • max_by requires an array as input, so in the spirit of your attempt, you might wish to consider:

    .files
    | map( select(.uri | contains("RELEASE")
                         and contains(".jar")))
    | max_by(.lastModified)
    

    p.s.

    In future, please follow the guidelines at http://stackoverflow.com/help/mcve