ryPaul
ryPaul

Reputation: 1

How can I use jq to sort this json file by the seconds value?

I have this json data:

{
    "results": [
        {
            "request": {
                "metric": "CLOUDFREE_TIME_PERCENT", 
                "path": {
                    "fromPoint": {
                        "pointX": -94.2802, 
                        "pointY": 23.6687, 
                        "pointZ": 564843.0
                    }, 
                    "time": {
                        "seconds": 1582074350
                    }, 
                    "toPoint": {
                        "pointX": -118.2258, 
                        "pointY": 33.7501, 
                        "pointZ": -2.6077032e-08
                    }
                }, 
                "waveLength": 0
            }, 
            "response": {
                "dataSources": [], 
                "metricValue": 0.0, 
                "success": false
            }
        },
        {
            "request": {
                "metric": "CLOUDFREE_TIME_PERCENT", 
                "path": {
                    "fromPoint": {
                        "pointX": -20.6361, 
                        "pointY": 16.1509, 
                        "pointZ": 563070.9
                    }, 
                    "time": {
                        "seconds": 1582056949
                    }, 
                    "toPoint": {
                        "pointX": -20.6361, 
                        "pointY": 16.1509, 
                        "pointZ": 563070.9
                    }
                }, 
                "waveLength": 0
            }, 
            "response": {
                "dataSources": [], 
                "metricValue": 0.0, 
                "success": false
            }
        }, 
        {
            "request": {
                "metric": "CLOUDFREE_TIME_PERCENT", 
                "path": {
                    "fromPoint": {
                        "pointX": -94.2661, 
                        "pointY": 23.6066, 
                        "pointZ": 564826.16
                    }, 
                    "time": {
                        "seconds": 1582064349
                    }, 
                    "toPoint": {
                        "pointX": -118.2258, 
                        "pointY": 33.7501, 
                        "pointZ": -2.6077032e-08
                    }
                }, 
                "waveLength": 0
            }, 
            "response": {
                "dataSources": [], 
                "metricValue": 0.0, 
                "success": false
            }
        } 
    ]
}

I have been trying (but failing) to sort this by the "seconds" value. I've tried jq '.results[].request.path.time|=sort_by(.seconds)' output.json and it gives me the error Cannot index number with string "seconds". I've tried all sorts of rearrangements of that command (and things like wrapping it with map()) as well to no avail. I'm starting to doubt if it's possible. Any help would be appreciated!

Upvotes: 0

Views: 227

Answers (1)

pmf
pmf

Reputation: 36151

You are trying to sort the time object which, of course, is not possible. Sort the array instead:

jq '.results |= sort_by(.request.path.time.seconds)' output.json

Upvotes: 1

Related Questions