下面是我的json文件中的前两项
{
"ReferringUrl": "N",
"OpenAccess": "0",
"Properties": {
"ItmId": "1694738780"
}
}
{
"ReferringUrl": "L",
"OpenAccess": "1",
"Properties": {
"ItmId": "1347809133"
}
}
我想通过出现在 json 中的每个 ItmId 来计算项目的数量。例如,在我的 json 文件中,带有“ItmId”1694738780 的项目出现了 10 次,带有“ItmId”1347809133 的项目出现了 14 次。然后像这样返回一个json
{"ItemId": "1694738780",
"Count": 10
}
{"ItemId": "1347809133",
"Count": 14
}
我正在使用 bash。并且更喜欢完全由 jq 来完成。但也可以使用其他方法。
谢谢!!!
最佳答案
这是一个解决方案(假设输入是有效的 JSON 对象流)并且您使用 -s 选项调用 jq:
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 有,则使用 inputs
是一种更节省内存的方法;但在这种情况下,请使用 -n 而不是 -s,并将上面的第一行替换为:[inputs | {ItemId: .Properties.ItmId} ]
高效解决方案
上述解决方案使用内置的group_by
,这很方便但导致容易避免的低效率。使用以下 counter
可以轻松编写非常有效的解决方案:
def counter(stream):
reduce stream as $s ({}; .[$s|tostring] += 1);
使用 -n 命令行选项,并应用如下:
counter(inputs | .Properties.ItmId)
这导致计数字典:
{
"1694738780": 1,
"1347809133": 1
}
这样的字典可能比 OP 设想的单例对象流更有用,但如果需要这样的流,可以按如下方式修改上面的内容:
counter(inputs | .Properties.ItmId)
| to_entries[]
| {ItemId: (.key), Count: .value}
关于json - jq通过特定的key统计json中的item个数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45170897/