我想统计一个特定值在JSON文件中列出了多少次。我的最终目标是真正获得一定的百分比。但是,我将首先进行计数。我敢肯定,我要比实际做的还要难,但是我对bash和jq还是陌生的。
"fields": [
{
"id": 1,
"state": "complete",
"substate": null,
"exceptions": [],
"name": "Sender Account Number",
"output_name": null,
"field_definition_attributes": {
"required": false,
"data_type": "Account Number",
"multiline": false,
"consensus_required": false,
"supervision_override": null
},
"transcription": {
"raw": "1685-0441-1",
"normalized": "168504411",
"source": "machine_transcription",
"data_deleted": false,
"user_transcribed": null,
"row_index": null
},
"field_image_url": "/api/v4/image/be167a88-9d1d-43bc-82b2-3d96d8c06656?start_x=0.3110429607297866&start_y=0.1052441592299208&end_x=0.5696909842243418&end_y=0.16043316955780607"
},
这是JSON中“字段”对象的示例。我想统计所有“来源”:“ machine_transcription”的记录,
然后,我应该能够计算出总额的百分比。
答案 0 :(得分:1)
如评论中所指出的,目前尚不清楚record
的含义,但以下仅jq的解决方案(假设输入JSON是带有名为“ fields”的键的对象,如问题所示)应该可以让您如愿以偿。请注意,输出为百分比。
def sigma( s ): reduce s as $x (0; . + $x);
def count( s ): sigma(if s then 1 else 0 end);
.fields
| length as $length
| count( .[]|.transcription.source == "machine_transcription" ) as $count
| if $length > 0 then $count*100/$length else null end
答案 1 :(得分:-2)
您可以将jq的输出传递给grep
,然后传递给wc
:
curl example.com | jq | grep '"source": "machine_transcription"' | wc -l
这将输出包含模式"source": "machine_transcription"