【问题标题】:How can I use jq to sort this json file by the seconds value?如何使用 jq 按秒值对这个 json 文件进行排序?
【发布时间】:2021-11-20 16:20:14
【问题描述】:

我有这个 json 数据:

{
    "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
            }
        } 
    ]
}

我一直在尝试(但失败)按“秒”值对其进行排序。我试过jq '.results[].request.path.time|=sort_by(.seconds)' output.json,它给了我错误Cannot index number with string "seconds"。我已经尝试了对该命令的各种重新排列(以及用map() 包装它之类的东西)也无济于事。我开始怀疑这是否可能。任何帮助将不胜感激!

【问题讨论】:

    标签: json sorting jq


    【解决方案1】:

    您正在尝试对time 对象进行排序,这当然是不可能的。而是对数组进行排序:

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

    【讨论】:

    • 哇!很简单。谢谢!我一直在关注该网站上其他类似问题的解决方案,但它们使我误入歧途。你帮我省了些麻烦。
    猜你喜欢
    • 2017-08-29
    • 1970-01-01
    • 2015-07-31
    • 2021-07-26
    • 2019-07-07
    • 2018-11-25
    • 2018-10-11
    • 2016-11-10
    • 1970-01-01
    相关资源
    最近更新 更多