0

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
  • 6,415
  • 13
  • 69
  • 135

2 Answers2

2

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

produces:

0.52

jpseng
  • 814
  • 9
0

You need try like:

.fruits |= sort_by(.version)

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

Reference sort by keys

Viettel Solutions
  • 1,037
  • 7
  • 13