【问题标题】:Sort an array of objects within an array by most recent timestamp, and then sort the outer array by each array's first object's timestamp, using jq按最近的时间戳对数组中的对象数组进行排序,然后使用 jq 按每个数组的第一个对象的时间戳对外部数组进行排序
【发布时间】:2020-09-26 16:33:14
【问题描述】:

使用jq,按照最近的时间戳对数组中的对象数组进行排序,然后按照每个数组的第一个对象的时间戳对外部数组进行排序。

这是 JSON 数据的一个示例,位于我被卡住的 jq 管道阶段。

[
  [
    {
      "created_at": "2020-09-26T14:48:46.000Z",
      "conversation_id": "1309867515456237571",
      "id": "1309867515456237571",
      "text": "example1"
    }
  ],
  [
    {
      "created_at": "2020-09-26T14:48:47.000Z",
      "conversation_id": "1309867518455156736",
      "id": "1309867518455156736",
      "text": "example2"
    },
    {
      "created_at": "2020-09-26T14:48:47.000Z",
      "conversation_id": "1309867518455156736",
      "id": "1309867517846810625",
      "text": "example3"
    },
    {
      "created_at": "2020-09-26T14:48:46.000Z",
      "conversation_id": "1309867518455156736",
      "id": "1309867516659937284",
      "text": "example4"
    }
  ],
  [
    {
      "created_at": "2020-09-26T14:48:48.000Z",
      "conversation_id": "1309867524473999364",
      "id": "1309867524473999364",
      "text": "example5"
    },
    {
      "created_at": "2020-09-26T14:48:47.000Z",
      "conversation_id": "1309867524473999364",
      "id": "1309867520468291586",
      "text": "example6"
    },
    {
      "created_at": "2020-09-26T14:48:47.000Z",
      "conversation_id": "1309867524473999364",
      "id": "1309867520153845760",
      "text": "example7"
    }
  ],
  [
    {
      "created_at": "2020-09-26T14:48:48.000Z",
      "conversation_id": "1309867524750749705",
      "id": "1309867524750749705",
      "text": "example8"
    }
  ]
]

我尝试过的一切都以这样的错误告终,

jq: error (at <stdin>:8): Cannot index string with string "created_at"

【问题讨论】:

    标签: json jq


    【解决方案1】:

    也许是这个?

    jq '.[] |= sort_by(.created_at) | sort_by(.[].created_at)'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-17
      • 1970-01-01
      • 2019-01-16
      • 1970-01-01
      相关资源
      最近更新 更多