jq通过特定键计算json中的项目数 [英] jq count the number of items in json by a specific key

查看:105
本文介绍了jq通过特定键计算json中的项目数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的json文件中的前两个项目

The following is the first two items in my json file

{
"ReferringUrl": "N",
"OpenAccess": "0",
"Properties": {
    "ItmId": "1694738780"
   }
}
{
"ReferringUrl": "L",
"OpenAccess": "1",
"Properties": {
    "ItmId": "1347809133"
  }
}

我想计算json中出现的每个ItmId的项数。例如,在我的json文件中,带有 ItmId 1694738780的项目出现10次,带有 ItmId 1347809133的项目出现14次。然后返回这样的json

I want to count the number of items by each ItmId appeared in the json. For example, items that with "ItmId" 1694738780 appears 10 times and items with "ItmId" 1347809133 appears 14 times in my json file. Then return a json like this

{"ItemId": "1694738780",
 "Count":  10
}
{"ItemId": "1347809133",
 "Count":  14
}

我正在使用bash。并且更喜欢完全由jq完成。

I am using bash. And prefer do this totally by jq. But it's ok to use other method.

谢谢!

推荐答案

这是一个解决方案(假设输入是有效的JSON对象的流),并且您使用-s选项调用jq:

Here's one solution (assuming the input is a stream of valid JSON objects) and that you invoke jq with the -s option:

map({ItemId: .Properties.ItmId})             # extract the ItmID values
| group_by(.ItemId)                          # group by "ItemId"
| map({ItemId: .[0].ItemId, Count: length})  # store the counts
| .[]                                        # convert to a stream

一种内存效率更高的方法是使用输入(如果您的jq有);但在这种情况下,请使用-n而不是-s,并用以下内容替换上面的第一行:[inputs | {ItemId:.Properties.ItmId}]

A slightly more memory-efficient approach would be to use inputs if your jq has it; but in that case, use -n instead of -s, and replace the first line above by: [inputs | {ItemId: .Properties.ItmId} ]

上述解决方案使用内置的 group_by ,这很方便,但容易导致效率低下。使用以下计数器可以轻松编写非常有效的解决方案:

The above solutions use the built-in group_by, which is convenient but leads to easily-avoided inefficients. Using the following counter makes it easy to write a very efficient solution:

def counter(stream):
  reduce stream as $s ({}; .[$s|tostring] += 1);

使用-n命令行选项,其应用如下:

Using the -n command-line option, and applied as follows:

counter(inputs | .Properties.ItmId)

这导致了一个计数字典:

this leads to a dictionary of counts:

{
  "1694738780": 1,
  "1347809133": 1
}

这样的词典可能比a词典更有用OP所设想的单例对象流,但是如果需要这样的流,则可以如下修改上述内容:

Such a dictionary is probably more useful than a stream of singleton objects as envisioned by the OP, but if such as stream is needed, one can modify the above as follows:

counter(inputs | .Properties.ItmId)
| to_entries[]
| {ItemId: (.key), Count: .value}

这篇关于jq通过特定键计算json中的项目数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆