首页 > 解决方案 > 用 jq 对 json 进行排序

问题描述

我有这个来自 REST api 的 json(简化):


{
  "data": [
    {
      "id": "run-bWSq4YeYpfrW4mx7",
      "type": "runs",
      "attributes": {
        "source": "tfe-configuration-version",
        "status": "planned",
        "status-timestamps": {
          "planned-at": "2017-11-28T22:52:51+00:00"
        }
    }
    },
    {
      "id": "run-bWSq4YeYpfrW4ft7",
      "type": "runs",
      "attributes": {
        "source": "tfe-configuration-version",
        "status": "planned",
        "status-timestamps": {
          "planned-at": "2017-11-28T21:52:51+00:00"
        }
    }
    }
    ]
}


我怎样才能对它进行排序status-timestamps.planned-at

除其他外,我尝试了这个jq '[.data[].attributes | sort_by(.status-timestamps.planned-at) ]'

标签: jsonrestsortingparsingjq

解决方案


这是否达到你想要的?

jq '.data |= sort_by(.attributes."status-timestamps"."planned-at")'

推荐阅读