2

There is an array of json object, using jq how to check if an object exists if so returns the true else false

I tried this but getting error

cat fruits.json | jq '.fruits[]| sort_by(.version)'

I would like to sort by decending order and output the price of the most recent version.

{
    "fruits": [
        {
            "name": "banana",
            "color": "yellow",
            "price": 0.51,
           "version": 1
        },
        {
            "name": "banana",
            "color": "yellow",
            "price": 0.52,
            "version": 2
        }
    ]
}
kumar
  • 8,207
  • 20
  • 85
  • 176

2 Answers2

3

cat fruits.json | jq '.fruits | sort_by(-.version)[0].price'

produces:

0.52

jpseng
  • 1,618
  • 6
  • 18
1

You need try like:

.fruits |= sort_by(.version)

Example: https://jqplay.org/s/ntjioYhKWq

Reference sort by keys

Viettel Solutions
  • 1,519
  • 11
  • 22