使用 jq 填充 json 数据并打印出现次数

使用 jq 填充 json 数据并打印出现次数

我试图用 jq 填充下面的 json 数据并期望输出如下

{
  "data": [
    {
      "topic_name": "BookShow",
      "topic_id": "ABCDFG",
      "urgency": "high"
    },
    {
      "topic_name": "AmzonMarket",
      "topic_id": "ESDCGHY",
      "urgency": "high"
    },
    {
      "topic_name": "AmzonMarket",
      "topic_id": "ESDCGHY",
      "urgency": "high"
    },
    {
      "topic_name": "BookShow",
      "topic_id": "ABCDFG",
      "urgency": "high"
    },
    {
      "topic_name": "bookTick",
      "topic_id": "KOLPUYDD",
      "urgency": "high"
    },
    {
      "topic_name": "bookTick",
      "topic_id": "KOLPUYDD",
      "urgency": "high"
    }
  ],
  "more": false,
  "limitations": 100,
  "range": 0
}

期望输出如下,这里“出现”将是新字段,用于计算出现次数。

"id","name","occurrences"
"KOLPUYDD","bookTick",2
"ABCDFG","BookShow",2
"ESDCGHY","AmzonMarket",2

请支持。

答案1

用于group_by形成所需的对象分组topic_id并从中创建 CSV。

jq --raw-output '[ "id", "name", "occurrences" ], ( .data | group_by(.topic_name)[] | 
  { id: .[0].topic_id, name: .[0].topic_name, occurrences: length } | 
    [.id, .name, .occurrences]) | @csv'

jqplay演示

答案2

使用jqonly 从数组中取出数据data,然后使用 Miller ( mlr) 来计算唯一条目:

$ jq '.data' file | mlr --ijson --ocsv uniq -c -g topic_id,topic_name,urgency
topic_id,topic_name,urgency,count
ABCDFG,BookShow,high,2
ESDCGHY,AmzonMarket,high,2
KOLPUYDD,bookTick,high,2

要删除该urgency字段并重新标记其余字段:

$ jq '.data' file | mlr --ijson --ocsv uniq -c -g topic_id,topic_name,urgency then cut -x -f urgency then label id,name,occurrences
id,name,occurrences
ABCDFG,BookShow,2
ESDCGHY,AmzonMarket,2
KOLPUYDD,bookTick,2

相关内容