Martin Thoma
Martin Thoma

Reputation: 136389

Can I get the argmax in jq?

The following dataset contains movies. I want to find the longest title with jq. What I got so far:

$ wget https://raw.githubusercontent.com/prust/wikipedia-movie-data/master/movies.json
$ cat movies.json | jq '[.[] | .title | length] | max'

So the longest title has 110 characters. The following query the shows it to me:

$ cat movies.json | jq '.[] | .title | select(length==110)' 
"Cornell-Columbia-University of Pennsylvania Boat Race at Ithaca, N.Y., Showing Lehigh Valley Observation Train"

Is it possible to directly get the argmax?

Background

I am currently trying what I can do with jq for exploratory data analysis. Usually, I would use Pandas for most of it. However, I recently had an example where jq was just super handy. So I want to learn more about it to see how far jq can go / where it is easier to use than Pandas.

Upvotes: 0

Views: 133

Answers (1)

oguz ismail
oguz ismail

Reputation: 50775

Yes, you can use max_by like:

max_by(.title | length).title

or,

map(.title) | max_by(length)

Upvotes: 2

Related Questions