Parsing JSON record-per-line with jq?

后端 未结 2 1111
被撕碎了的回忆
被撕碎了的回忆 2021-01-17 13:42

I\'ve got a tool that outputs a JSON record on each line, and I\'d like to process it with jq.

The output looks something like this:

{\"         


        
2条回答
  •  青春惊慌失措
    2021-01-17 14:28

    Use the --slurp (or -s) switch:

    ./tool | jq --slurp 'group_by(.id)'
    

    It outputs the following:

    [
      [
        {
          "ts": "2017-08-15T21:20:47.029Z",
          "id": "123",
          "elapsed_ms": 10
        }
      ],
      [
        {
          "ts": "2017-08-15T21:20:47.044Z",
          "id": "456",
          "elapsed_ms": 13
        }
      ]
    ]
    

    ...which you can then process further. For example:

    ./tool | jq -s 'group_by(.id) | map({id: .[0].id, count: length})'
    

提交回复
热议问题