代码之家  ›  专栏  ›  技术社区  ›  J. M. Becker

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

  •  0
  • J. M. Becker  · 技术社区  · 4 年前

    使用以下命令按最新时间戳对数组中的对象数组进行排序,然后按每个数组的第一个对象的时间戳对外部数组进行排序 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"
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   user197693    4 年前

    也许是这个?

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